This content has been marked as final.
Show 8 replies
-
1. Re: Java Regex Engine question - Conditional Statements?
796254 Jun 4, 2008 2:26 PM (in response to 807591)Maybe a more Perl-like regex implementation as a 3rd party JAR would help. Maybe Jakarta ORO can manage it.
My understanding is that the regex built into the JDK was as close as we'll come, so you might be out of luck.
I'm not enough of a regex maven. I've never seen that syntax before.
% -
2. Re: Java Regex Engine question - Conditional Statements?
807591 Jun 4, 2008 2:39 PM (in response to 796254)Cheers Duffy.
Unfortunatley changing the regex engine is not a possibility. (I am not actually developing the app, just passing arguments to it).
I am pretty new to regular expressions myself. Hopefully there is a way to match what I want to without using a conditional statement but I am finding it hard. -
3. Re: Java Regex Engine question - Conditional Statements?
800282 Jun 4, 2008 2:43 PM (in response to 807591)steve_s wrote:
Something like this should work:
Hi guys
I am having difficulty writing a conditional regular expression using Java regex implementation.
I have read that the java regex engine does not implement the conditional statement that is present in Perls regex engine.
for example in Perl you can have (?(?=regex)then|else)
The application can recieve a string in anyone of these 4 formats and a single regular expression is needed to match MyMatch in each instance.
1. Blade=0-3,MyMatch=1
2. Blade=0-3,MyMatch.attribute=1
3. MyMatch=1
4. MyMatch.attribute=1
I can do this with the following regular expression using the Perl engine.
(?(.+,).+,([^\.]*).*=.+|([^\.]*).*=.+)
But I am completely stumped as to how I can do it with Java as the conditional statement does not seem to be implemented.
Any help greatly appreciated.
Steve
In other words:String[] tests = { "Blade=0-3,MyMatch=1", "Blade=0-3,MyMatch.attribute=1", "MyMatch=1", "MyMatch.attribute=1" }; Pattern p = Pattern.compile("[^=,.]+(?=[^,]+$)"); for(String t: tests) { Matcher m = p.matcher(t); System.out.println(m.find() ? m.group() : ""); }
[^=,.]+ match one or more characters which are not '=', ',' or '.' (?=[^,]+$) only if there is no single comma in front of it (until the end of the String!)
-
4. Re: Java Regex Engine question - Conditional Statements?
807591 Jun 4, 2008 3:03 PM (in response to 800282)Thank you very much prometheuzz. I appreciate the reply.
Just to clarify. I am just passing the expression to a method via a form. I am not coding anything nor do have I access to the code.
I have tried to verify your regular expression using this applet
http://www.cis.upenn.edu/~matuszek/General/RegexTester/regex-tester.html
The applet has both a matches() and find() method for testing. The matches() method returns no match, but the finds() method works? I am unsure of the difference between the two? Maybe you could enlighten me? Normally it would be the matches method that i would use to verify if the regex will work.
Unfortunately using this regex expression the parser returns a warning for each string.
Edited by: steve_s on Jun 4, 2008 8:03 AMWARNING String Blade=0-3,Cp.attribute=2 doesn't match defined regExp [^=,.]+(?=[^,]+$) WARNING String Blade=0-6,Cp=2 doesn't match defined regExp [^=,.]+(?=[^,]+$) WARNING String Cp=2 doesn't match defined regExp [^=,.]+(?=[^,]+$) WARNING String Cp.attribute=2 doesn't match defined regExp [^=,.]+(?=[^,]+$)
-
5. Re: Java Regex Engine question - Conditional Statements?
800282 Jun 4, 2008 3:28 PM (in response to 807591)steve_s wrote:
It is not clear to me what happens with the regex after the form.
Thank you very much prometheuzz. I appreciate the reply.
Just to clarify. I am just passing the expression to a method via a form. I am not coding anything nor do have I access to the code.
I have tried to verify your regular expression using this applet
No, there IS a match (returned by find()) but matches() tries to match the entire String.
http://www.cis.upenn.edu/~matuszek/General/RegexTester/regex-tester.html
The applet has both a matches() and find() method for testing. The matches() method returns no match,
but the finds() method works? I am unsure of the difference between the two? Maybe you could enlighten me?
As I said above: in Java, the Matcher.matches() (or String.matches(regex)) method will evaluate to true iff the entire input String matches the regex. The Matcher.find() followed by the Matcher.group() will return only that part of the input String that matches the regex.
Normally it would be the matches method that i would use to verify if the regex will work.
Like I said: matches() and find() are two different things. If you're going to use find() it makes no sense to verify the regex with matches().
Unfortunately using this regex expression the parser returns a warning for each string.
Edited by: steve_s on Jun 4, 2008 8:03 AMWARNING String Blade=0-3,Cp.attribute=2 doesn't match defined regExp [^=,.]+(?=[^,]+$) WARNING String Blade=0-6,Cp=2 doesn't match defined regExp [^=,.]+(?=[^,]+$) WARNING String Cp=2 doesn't match defined regExp [^=,.]+(?=[^,]+$) WARNING String Cp.attribute=2 doesn't match defined regExp [^=,.]+(?=[^,]+$)
Perhaps I'm missing something... -
6. Re: Java Regex Engine question - Conditional Statements?
800282 Jun 4, 2008 3:34 PM (in response to 800282)Note that the regex
will cause matches() to return true, but then group() will return the entire input String and group(1) will return the substring you're interested in."^(?:[^,]+,)?([^=,.]+)(?=[^,]+$).*"
-
7. Re: Java Regex Engine question - Conditional Statements?
807591 Jun 4, 2008 3:43 PM (in response to 800282)Thank you very much. That works.
It was the group(1) match that I was after.
Sorry I was unsure myself of how the parser handled the regex. -
8. Re: Java Regex Engine question - Conditional Statements?
800282 Jun 4, 2008 4:29 PM (in response to 807591)steve_s wrote:
No problem. Good to hear it's sorted, and you're welcome.
Thank you very much. That works.
It was the group(1) match that I was after.
Sorry I was unsure myself of how the parser handled the regex.