0

I need to find a substring in a sentence "Hello, call me XXX.". The sentence can be really long and the only thing that helps me to identify what the name is, is the fact that name is always in fromat "call me"+space+name"+dot. Nevertheless the sentence could also look like hello, call me. call me xxx.

Call me John. ⇒ John

Call me Call me John. ⇒ prohibited - confusing

Call me. Call me John. ⇒ John

Call me  Call me John. ⇒ John

Call me Peter .Call me John. ⇒ John

Call me Peter. Call me John. ⇒ prohibited - more then one name...

Name can be any sequence of chars except \r, \n, \0 and a dot.

I would appreciate it If somebody could help me to define the regex. I'm trying to figure out for more than two hours but without any success...

3
  • 3
    2nd and 4th sentences are same and you expect different output. Ambiguity!! Commented Feb 15, 2013 at 13:17
  • It's only because of bad formatting here on stackoverflow...in fourth sentence there should be two spaces after first "Call me" ...but yeah...my bad..I should write it somehow to make it clearer :) Commented Feb 15, 2013 at 13:20
  • Why Call me Call me John. is prohibited? According to your problem statement, it should be OK and name is Call me John. Also why Call me Peter .Call me John. is non prohibited? There are two matches: with name Peter and name John. In case name cannot contain spaces, what is the problem with Call me Call me John.? It is not confusing and name is John. Commented Feb 15, 2013 at 13:20

3 Answers 3

2

regex should work for you:

"(?<=call me )[^.]*"
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming that name cannot contain spaces:

String string = "Call me Peter .Call me John.";
Matcher matcher = Pattern.compile ("Call me ([^\r\n\0\\. ]+)\\.").matcher (string);
if (matcher.find ())
{
    String name = matcher.group (1);
    if (matcher.find ()) throw new Exception ("Prohibited: too many matches!");
    System.out.println (name);
}
else throw new Exception ("Prohibited: no matches!");

Comments

0

Somethings like this: .*Call\ me\ (.[\w]+).?

Check if it fullfils all your requirements on-line at: http://www.rubular.com/

Comments

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.