7 Replies Latest reply: Nov 22, 2012 2:45 AM by AjayL RSS

    Extract a word using Regex.

    AjayL
      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
          Hint: there are TWO spaces in your sample strings...
          • 2. Re: Extract a word using Regex.
            sabre150
            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
              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
                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
                  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
                    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
                      Thank you Sabareesh. I had just missed out on the * after the \\s.
                      Lesson learnt.
                      Thank you for your help.