i am new to regular expressions in Java. I like to extract a string by using regular expressions.
This is my String: “Hello,World”
I like to extract the text after “,”. The result would be “World”. I tried this:
final Pattern pattern = Pattern.compile(",(.+?)");
final Matcher matcher = pattern.matcher("Hello,World");
matcher.find();
But what would be the next step?
You don’t need Regex for this. You can simply split on comma and get the 2nd element from the array: –
OUTPUT: –
But if you want to use
Regex, you need to remove?from your Regex.?after+is used forReluctantmatching. It will only matchWand stop there.You don’t need that here. You need to match until it can match.
So use
greedymatching instead.Here’s the code with modified Regex: –
OUTPUT: –