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
    Oct 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with Regex

    Does anyone know how to use regex to capture patterns?

    I am coming from many years of php background, in php its pretty much straight forward preg_match($pattern, $subject, $matches);
    and then you get all your matches in the $matches array

    How do I do somethink like that in Java?
    Let's say I want to capture words
    'truck' and 'old' from the string "old man trucking in an old truck'
    What I need it to get back array or set with words 'truck' and 'old' but not trucking
    I would use the \w modifier to make sure I capture on word boundries

    Can anyone help me here?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help with Regex

    See Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes). The String class offers a few functions to replace and match using regular expressions, but capturing groups is best done using a Pattern and Matcher class described in the above link.

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

    Default Re: Need help with Regex

    The OP probably doesn't need it, but for reference of others who happen by this thread when searching for help with regex: another very good learning resource is
    Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns

    db

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Regex

    Thanks. I figured out how to use Pattern and Matcher. It's a lot more involved than in PHP
    I find that extracting matches based on regex is the easiest in php, slightly more code required in Javascript and even more code required in Java.

    This is sample of my test code:
    String text = "Some text about cats, dogs, cat and dog and another *cat*";
    Pattern pattern;
    TreeSet<String> matches = new TreeSet<String>();
    pattern = Pattern.compile("\\bcat\\b|\\bdog\\b", Pattern.CASE_INSENSITIVE);
    		Matcher matcher = pattern.matcher(text);
    		while (matcher.find()) {
    			System.out.println("found it");
    			matches.add(matcher.group());
    		}
     
    		for (String m : matches) {
    			System.out.println("Found: " + (String) m);
    		}

    By using TreeSet I am filtering out duplicates.
    I wonder what do you, experienced Java programmer think? Is this about the right way to collect matches or is there an easier way out there?

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Need help with Regex

    Take a look at this link - http://www.javaprogrammingforums.com...explained.html
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Help with regex pattern
    By b_jones10634 in forum Java Theory & Questions
    Replies: 4
    Last Post: September 24th, 2010, 03:59 PM
  2. Need help with regEx
    By ptabatt in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 14th, 2010, 11:17 AM
  3. [SOLVED] Need Regex for a pattern
    By mallikarjun_sg in forum Java Theory & Questions
    Replies: 7
    Last Post: May 5th, 2010, 02:06 AM
  4. Possible regex problem?
    By Bill_H in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 25th, 2009, 01:36 AM
  5. Regex Question
    By igniteflow in forum Java SE APIs
    Replies: 1
    Last Post: August 28th, 2009, 11:46 AM