How to get String after a certain character using pattern matching?

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.

Answers


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"

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

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

You can do it in one line:

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

Need Your Help

Modify Flash movie without fla files or decompilers

flash actionscript

I have been asked to add just some more text in the last "slide" of a Flash movie. I don't have access to the .fla files. What are my options (apart from using decompilers)? Many thanks.

Install Subliminal through cocoa pods

ios xcode testing cocoapods subliminal

How to install Subliminal through cocoa pods?? Please, help)))

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.