5
String tect = "A to B";
Pattern ptrn = Pattern.compile("\\b(A.*)\\b");
Matcher mtchr = ptrn.matcher(tr.text()); 
while(mtchr.find()) {
    System.out.println( mtchr.group(1) );
}

I am getting output A to B but I want to B.

Please help me.

0

3 Answers 3

3

You can just place the A outside of your capturing group.

String s  = "A to B";
Pattern p = Pattern.compile("A *(.*)");
Matcher m = p.matcher(s);
while (m.find()) {
  System.out.println(m.group(1)); // "to B"
}

You could also split the string.

String s = "A to B";
String[] parts = s.split("A *");
System.out.println(parts[1]); // "to B"
Sign up to request clarification or add additional context in comments.

Comments

1

Change your pattern to use look-behind possitive assertion checking for A:

Pattern ptrn = Pattern.compile("(?<=A)(.*)");

1 Comment

That's some scary stuff.
0

You can do it in one line:

String afterA = str.replaceAll(".*?A *", ""),

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.