need a help with regular expression

hi

how i can write a pattern that match the following string:

<JFrame name="MyFrame" id="1" location="1,1">

in the way that the attributes (name, id, location) can come in different order and all of them must be in the string, and just one time each of the attributes.

which mean that the pattern should match:

<JFrame name="MyFrame" id="1" location="1,1">

<JFrame id="1" name="MyFrame" location="1,1">

<JFrame name="MyFrame" location="1,1" id="1">

...

i wrote this pattern, but it isn't match the string correctly:

Pattern pJFrame = Pattern.compile("<JFrame((\\s+id\\s*=\\s*\"(\\d+)\")|(\\s+name\\s*=\\s*\"(\\w+)\")|(\\s+location\\s*=\\s*\"(\\d*),(\\d*)\")){3}\\s*>");

the pattern i wrote can't guaranty that every attribute appears just one time in the string. so it match other strings like ( <JFrame name="MyFrame" id="1" id="1"> ) that should not be matched.

Thanks in Advance,

[1324 byte] By [dejavu22a] at [2007-10-3 10:57:26]
# 1
I don't think you can do that with regex unless you explicitly specify all the permutations of the order. I look forward to sabre or unky proving me wrong, though.Really, this is a job for an XML parser.
jverda at 2007-7-15 6:23:31 > top of Java-index,Java Essentials,Java Programming...
# 2

I can't chek that all the attributes are present and then only once but I can extract the attributes using

final String line = "JFrame name=\"MyFrame\" id=\"1\" location=\"1,1\">";

final Pattern p = Pattern.compile("(name|id|location)=\"([^\"]*)\"");

final Matcher m = p.matcher(line);

while (m.find())

{

System.out.println(m.group(1) + "=[" + m.group(2)+"]");

}

'uncle_alice' may be able to provide a solution.

sabre150a at 2007-7-15 6:23:31 > top of Java-index,Java Essentials,Java Programming...
# 3

import java.util.regex.*;

public class Test

{

public static void main(String[] args)

{

String[] str = {"<JFrame name=\"MyFrame\" id=\"1\" location=\"1,1\">",

"<JFrame id=\"1\" name=\"MyFrame\" location=\"1,1\">",

"<JFrame name=\"MyFrame\" location=\"1,1\" id=\"1\">",

"<JFrame name=\"MyFrame\" id=\"1\">",

"<JFrame name=\"MyFrame\" foo=\"bar\" location=\"1,1\" id=\"1\">",

"<JFrame name=\"MyFrame\" id=\"1\" location=\"1,1\" id=\"1\">"};

Pattern p = Pattern.compile("<JFrame (name|id|location)=\"[^\"]*\" "

+ "(?!\\1)(name|id|location)=\"[^\"]*\" "

+ "(?!\\1|\\2)(name|id|location)=\"[^\"]*\">");

Matcher m = p.matcher("");

for (String s : str)

{

System.out.printf("%-60s%b%n", s, m.reset(s).matches());

}

}

}

uncle_alicea at 2007-7-15 6:23:31 > top of Java-index,Java Essentials,Java Programming...
# 4
I was wondering if back-references might be useful. I wouldn't have thought to combine that with the zero-width negative lookahead though.Clever.Gross, but clever.
jverda at 2007-7-15 6:23:31 > top of Java-index,Java Essentials,Java Programming...
# 5
:-) Very clever uncle_alice. I played around for half an hour but failed to get even close to this.
sabre150a at 2007-7-15 6:23:31 > top of Java-index,Java Essentials,Java Programming...
# 6
Thank all of you for the help... uncle_alice, thanks for your great solution, that what i was looking for..
dejavu22a at 2007-7-15 6:23:31 > top of Java-index,Java Essentials,Java Programming...
# 7
> Gross, but clever.Yeah, when you find yourself having to be this clever, it's time to start thinking seriously about using a dedicated tool like an XML parser instead of regexes. :-)
uncle_alicea at 2007-7-15 6:23:31 > top of Java-index,Java Essentials,Java Programming...