Re: RegEx Help one more time



Ken Kast writes:

Here's my pattern:
pattern =
"\\b[A-Z]([A-Z0-9]|[-+_/&.](?=[A-Z0-9])|[(][A-Z0-9]([A-Z0-9]|[-+_/&.](?=[A-Z0-9]))*[)])+\\b";

With the string AB(CDE), it finds only AB(CDE. Why isn't the closing
parens found and why is it accepting the string without it?

I submit the following little program as an example of how you can
study such problems yourself in a kind of experimental way. However,
it doesn't find "AB(CDE", nor do I see how it could when the branch
that matches the opening "(" ends with the closing ")".

To match the closing paren before a word boundary there has to be a
word character immediately after it.

import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Foo {
public static void main(String [] _) {
String pattern = "\\b[A-Z]("
+ "[A-Z0-9]"
+ "|[-+_/&.](?=[A-Z0-9])"
+ "|[(][A-Z0-9]([A-Z0-9]|[-+_/&.](?=[A-Z0-9]))*[)]"
+ ")+\\b";

String text = "First AB(CDE) ends at a non-word-boundary, "
+ "second GH(IJ)K ends at a word-boundary."
+ "Third LM(NOP)q should be caught.";

Matcher m = Pattern.compile(pattern).matcher(text);
while (m.find()) {
System.out.println(m.group());
}
}
}
.



Relevant Pages

  • Re: modules and passing variables
    ... Public Sub CloseThisAndOpenThat (stDocName As String) ... you don't need the parens here either: ...
    (microsoft.public.access.modulesdaovba)
  • Re: Confusing POST behavior -- doing it twice?
    ... To compare strings you use strcmp (or an ... It isn't all in parens, ... > pasting *actual* code into your Usenet post or is this hand typed? ... > numeric thing might actually be a string. ...
    (comp.lang.php)
  • Re: modules and passing variables
    ... Public Sub CloseThisAndOpenThat (stDocName As String) ... you don't need the parens here either: ...
    (microsoft.public.access.modulesdaovba)
  • Re: Can one function get anothers ParamArray?
    ... The Excel Calculation Site ... Because I've just written the code to parse a function's param string. ... String w/out parens, and/or comma-delimited params only, a piece of ...
    (microsoft.public.excel.programming)
  • Re: Style question
    ... >> parens dangling on their own lines. ... >> the closing paren of the last FORMAT expression. ... > extra expressions for debbuging purposes if closing parens are ... Certainly I clean it up if I'm going to post it to a lisp newsgroup;) ...
    (comp.lang.lisp)