Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Using Regular Expression (regex) in Java Programming

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using Regular Expression (regex) in Java Programming

    Hiii all,

    I'm new in using Regular Expression in Java.

    For example, I have String s like this:

    String s = "Why John Smith and Alan Smith and Nick Gates are the same?"

    How can I get sub-strings as "John Smith", "Alan Smith", "Nick Gates" - (names of people - with first upper character) from s by using regex?

    I try to use regex in Java, but It totally doesn't work.

    This is my code:
    /*
    String EXAMPLE_TEST = "Why John Smith and Alan Smith and Nick Gates are the same?";
    System.out.println(EXAMPLE_TEST.matches("([A-Z]&&[a-z])"));
    String[] splitString = (EXAMPLE_TEST.split("([A-Z]&&[a-z])"));
    System.out.println(splitString.length);
    for (String string : splitString) {
    System.out.println(string);
    }

    */


    Please help me to have exact "regex" in this case.

    Thanks all in advance!
    Last edited by helloworld922; May 13th, 2010 at 09:42 AM. Reason: please use [code] tags


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Using Regular Expression (regex) in Java Programming

    If all you want are words that begin with capital letters, it's much easier to not use your own regex and simply use the Scanner.next() method to get the word, at which point you can check to see if the first letter is upper case.

    Scanner reader = new Scanner(EXAMPLE_TEST); // using the string you provided
    while (reader.hasNext())
    {
         String read = reader.next();
         if (Character.isUpperCase(read.charAt(0))
         {
              System.out.println("Found a name: " + read);
         }
    }

    There are going to be a few problems with this basic code, such as it will pick up the first letter of the sentence, and it will also pick up abbreviations/other proper nouns that happen to have the first letter capitalized. Unfortunately, there's not really a way to get around this problem without doing some pretty advanced contenxt handling and/or natural language processing.

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Using Regular Expression (regex) in Java Programming

    This will be pretty hard I think unless you can always ignore the first word, otherwise you'd end up getting a match on "Why John" which is not supposed to happen just like explained by helloworld.

    You'd need to have a dictionary of commons words to exclude if you'd like to make it more fool proof and maybe even a dictionary of known names and if the word matches a name you can safely say its a name

    // Json

  4. #4
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using Regular Expression (regex) in Java Programming

    hii all,

    Thanks for your opinions,
    I figured out by using regex like this: " [A-Z][a-z]+ [A-Z][a-z]+"

    This is my code:

    /*
    Pattern pattern = Pattern.compile(" [A-Z][a-z]+ [A-Z][a-z]+");
    Matcher matcher = pattern.matcher("Why John Smith and Alan Smith and Nick Gates are the same?");
    while( matcher.find() )
    System.out.println("main: "+matcher.group());
    */

    print out:

    main: John Smith
    main: Alan Smith
    main: Nick Gates

  5. #5
    Junior Member
    Join Date
    May 2010
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Using Regular Expression (regex) in Java Programming

    will it work for the string
    Why John smith and Alan Smith and Nick gates are the same?

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Using Regular Expression (regex) in Java Programming

    So basically you expect every name to have a whitespace in front of it as well. Fair enough, this might work

    // Json

  7. #7
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Using Regular Expression (regex) in Java Programming

    The OP has cross-posted on CodeGuru forums.

  8. #8
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Using Regular Expression (regex) in Java Programming

    And you've obviously cross-read it

    // Json

  9. #9
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Using Regular Expression (regex) in Java Programming

    Quote Originally Posted by Json View Post
    And you've obviously cross-read it
    Just a heads-up for anyone who feels they may be wasting their time on a question that may have already been answered elsewhere...

Similar Threads

  1. Urgent need a java regex pattern
    By mallikarjun_sg in forum Java Theory & Questions
    Replies: 1
    Last Post: May 6th, 2010, 06:51 AM
  2. Illegal Start of Expression Error. Any help is appreciated.
    By SKhoujinian91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 8th, 2009, 12:57 AM
  3. Help creating expression tree
    By nellaf in forum Java SE APIs
    Replies: 1
    Last Post: December 3rd, 2009, 08:54 AM
  4. Creating an Expression Tree
    By vluong in forum Collections and Generics
    Replies: 0
    Last Post: November 28th, 2009, 11:41 PM
  5. [SOLVED] Java Regular Expressions (regex) Greif
    By username9000 in forum Java SE APIs
    Replies: 4
    Last Post: June 11th, 2009, 06:53 PM