0

I want to extract word after first 'from' in the following string-

"anti-CD24 from Chemicon, and anti-CD48 from Santa"

Right now, I am using -

"from\s+([^\s]+)"

But it is giving both results 'Chemicon' and 'Santa' instead of only 'Chemicon'.

2
  • 4
    Then just use the first result? Commented Jun 30, 2012 at 11:08
  • how to extract out the first result? I am using if (m.find()) and it is giving both results instead of first one. Commented Jun 30, 2012 at 11:12

2 Answers 2

1

Maybe you could do something like this:

    Pattern p = Pattern.compile("from\\s+([^\\s]+)");
    Matcher m = p.matcher("anti-CD24 from Chemicon, and anti-CD48 from Santa");
    if (m.find()) {
        System.out.println(m.group(1));
    }

This would take just the first one. And maybe you could use this regular expression to avoid the comma for the first match (Chemicon,): from\\s+([a-zA-Z]+)

Sign up to request clarification or add additional context in comments.

2 Comments

Gosh! Was gonna type the very same code after I just implemented it in my Eclipse.. +1 for being faster than me lol
thanx, but i am using like anti-CD24.*from\\s+([^\\s]+), in which case it is actually matching the last one ie. 'Santa', but i want to extract 'Chemicon,'
0

You can use the lookingAt method on the Method object. It stops after it finds the first match.

Like the matches function and unlike the find function lookingAt has to match from the beginning, so we have to use the regex ".*?from\\s+(\\w+)", which says: match anything up until the first "from", then one or more whitespace chars, then the next "word". If you only want alphabetical chars to match your word after "from" then use ".*?from\\s+([a-zA-Z])". The ".*?" at the beginning means non-greedy matching. If you just use ".*" it will match the last "from", not the first one.

Here's code that I've tested:

  String s = "anti-CD24 from Chemicon, and anti-CD48 from Santa";
  Matcher m = Pattern.compile(".*?from\\s+(\\w+)").matcher(s);
  if (m.lookingAt()) {
    System.out.println(m.group(1));
  }

Prints out:

Chemicon

2 Comments

thanx, it even worked with using find, after i used .*? instead of .*
Yes, good catch - that also works in this case. Because we use non-greedy matching of any character that would make find work like lookingAt in this scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.