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]

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.
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());
}
}
}