This discussion is archived
7 Replies Latest reply: Nov 22, 2012 12:45 AM by AjayL RSS

Extract a word using Regex.

AjayL Newbie
Currently Being Moderated
Hi

I am trying to extract a word after the "=".

Example :

String str = " Book = Java ";
Output : Java

String str2 = "Book = 235Java ";
Output : 235Java

I am trying the code
String criteria = "Book = Java     ";


regex = "([a-z]+=)(\\w+)";               // a-z is group 1

pattern = Pattern.compile(regex);
matcher = pattern.matcher(criteria);

System.out.println(matcher.group(2));   // Get the word after =
Any suggestions please? Help appreciated



  • 1. Re: Extract a word using Regex.
    gimbal2 Guru
    Currently Being Moderated
    Hint: there are TWO spaces in your sample strings...
  • 2. Re: Extract a word using Regex.
    sabre150 Expert
    Currently Being Moderated
    and the groups will only be available after a call to matcher.matches() or matcher.find() that returns 'true' .
  • 3. Re: Extract a word using Regex.
    AjayL Newbie
    Currently Being Moderated
    Ok.So I have changed my regex pattern to give me the word after the =
    But this is not giving me the word after the equal sign
    Strinf criteria = "Book     =       Java     "
    
    String regexSecondWord = "(=)(\\w+)";    // Give me the word after =
    
    Pattern patternSecondWord = Pattern.compile(regexSecondWord);
    Matcher matcherSecondWord = patternSecondWord.matcher(criteria);
    
    System.out.println(matcherSecondWord.group());  // null
    matcherSecondWord.group(2)                           // null
    Please advise.
  • 4. Re: Extract a word using Regex.
    SabareeshDivakaran Newbie
    Currently Being Moderated
    Try below code
            String criteria = "     Book     =          Java         ";
            String regexSecondWord = "(=)(\\s*)(\\w+)";
            Pattern patternSecondWord = Pattern.compile(regexSecondWord);
            Matcher matcherSecondWord = patternSecondWord.matcher(criteria);
            if (matcherSecondWord.find()) {            
                System.out.println(matcherSecondWord.group(2) + matcherSecondWord.group(3));
            } else {
                System.out.println("nomatch");
            }
    If you don't need leading space in " Java " use matcherSecondWord.group(3) alone.

    Thanks,
    Sabareesh.
  • 5. Re: Extract a word using Regex.
    gimbal2 Guru
    Currently Being Moderated
    Sabareesh Divakaran wrote:
    Try below code
    "In stead of teaching a man how to fish so that he may provide for himself, you just threw him a fish".
  • 6. Re: Extract a word using Regex.
    sabre150 Expert
    Currently Being Moderated
    gimbal2 wrote:
    Sabareesh Divakaran wrote:
    Try below code
    "In stead of teaching a man how to fish so that he may provide for himself, you just threw him a fish".
    A smelly fish at that since the regex is over-complicated.
  • 7. Re: Extract a word using Regex.
    AjayL Newbie
    Currently Being Moderated
    Thank you Sabareesh. I had just missed out on the * after the \\s.
    Lesson learnt.
    Thank you for your help.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points