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 5 of 5

Thread: Need help with regEx

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

    Question Need help with regEx

    How exactly do regular expressions work in Java? Let's say I have the string:

    String test = new String("Sally went to the barn.");
    //any string will do this is for experimentation purposes

    and I wanted to use the String's split() function... or any regEx function to get all the words out and put them into a neat little ArrayList<String> (or even better a Hashtable<String, Integer> that'll count the amount of times each word appears).

    I tried:
    String regEx = "\w";
    String[] splitter = test.split(regEx);
     
    for(int i=0;i<splitter.length;i++){
    			System.out.println(splitter[i]);
    		}

    But the compiler tells me that "\w" is "an invalid escape sequence. How would I go about extracting every word from a string and putting it into a data structure? Please help, I'm so lost.
    Last edited by ptabatt; August 13th, 2010 at 12:59 PM. Reason: variable mismatch


  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: Need help with regEx

    When you type into the code '\w', it's looking for a single character code which matches that escape character string (which doesn't exist). If you want the Java to use "\w" as a string,you must provide the escape character for the back-slash.

    String regEx = "\\w"; // notice the two back-slashes, while in the code it will look like this the string will actually represent "\w" (two characters)
    String[] splitter = test.split(regEx);
    //... rest of code

  3. #3
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Need help with regEx

    instead of "\w" you can just use a space.
     
    String[] splitter = test.split(" ");
     
    for(int i=0;i<splitter.length;i++){
    			System.out.println(splitter[i]);
    		}

    If you dont want the period in the token just remove the period(s) before you tokenize it.
    test=test.replace(".", "");
    String[] splitter = test.split(" ");
     
    for(int i=0;i<splitter.length;i++){
    			System.out.println(splitter[i]);
    		}
    Last edited by Brt93yoda; August 13th, 2010 at 06:05 PM.

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    495
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Need help with regEx

    Quote Originally Posted by Brt93yoda View Post
    instead of "\w" you can just use a space.
    The word character and a space are not mutually interchangeable.

    If you dont want the period in the token just remove the period(s) before you tokenize it.
    test=test.replace(".", "");
    String[] splitter = test.split(" ");
     
    for(int i=0;i<splitter.length;i++){
    			System.out.println(splitter[i]);
    		}
    1. Where did you get that requirement from?
    2. The OP's question was about regexes. String#replace has nothing whatsoever to do with the question.

    It appears that you have blindly posted answers you know even though they don't relate to the question asked.

    db

  5. #5
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Need help with regEx

    Sorry about that.

Similar Threads

  1. Urgent need a java regex pattern
    By mallikarjun_sg in forum Java Theory & Questions
    Replies: 1
    Last Post: May 6th, 2010, 05:51 AM
  2. [SOLVED] Need Regex for a pattern
    By mallikarjun_sg in forum Java Theory & Questions
    Replies: 7
    Last Post: May 5th, 2010, 02:06 AM
  3. Possible regex problem?
    By Bill_H in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 25th, 2009, 01:36 AM
  4. Regex Question
    By igniteflow in forum Java SE APIs
    Replies: 1
    Last Post: August 28th, 2009, 11:46 AM
  5. [SOLVED] Java Regular Expressions (regex) Greif
    By username9000 in forum Java SE APIs
    Replies: 4
    Last Post: June 11th, 2009, 05:53 PM