Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

RegEx Help one more time

Thread view: 
Ken Kast - 24 Feb 2007 15:55 GMT
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?

Thanks.

Ken
Jussi Piitulainen - 24 Feb 2007 17:38 GMT
> 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());
    }
   }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.