Reputation: 351
I am trying to match the following pattern in a string and extract it as a substring.
The pattern always follows
<D-10-helloworld-84>
The 'D'
can either be 'D'
or 'E'
but nothing else. the message in the middle can have any character and each number is always a digit decimal (5
would 05
for example).
I have tried the following:
String text = "sdsas<D-10-helloworld-84>kjvkjv";
Pattern pattern = Pattern.compile("^<[ED]-[0-9]{2}-(.?)-[0-9]{2}>$");
Matcher matcher = pattern.matcher(noiseFrame);
String newText = matcher.group(1);
But not match is being found. What's happening here? Thanks
Upvotes: 1
Views: 1394
Reputation: 42020
Try the next regular expression:
.*<[ED]-[0-9]{2}-([^-]+)-[0-9]{2}>.*
e.g.:
String text = "sdsas<D-10-helloworld-84>kjvkjv";
String newText = text.replaceAll(".*<[ED]-[0-9]{2}-([^-]+)-[0-9]{2}>.*", "$1");
Using a constant:
private static final Pattern REGEX_PATTERN =
Pattern.compile(".*<[ED]-[0-9]{2}-([^-]+)-[0-9]{2}>.*");
public static void main(String[] args) {
String input = "sdsas<D-10-helloworld-84>kjvkjv";
System.out.println(
REGEX_PATTERN.matcher(input).matches()
); // prints "true"
System.out.println(
REGEX_PATTERN.matcher(input).replaceFirst("$1")
); // prints "helloworld"
}
Upvotes: 0
Reputation: 785146
Since your input has some text before/after < and >
you need to remove anchors ^ and $
from your regex:
Pattern pattern = Pattern.compile("<[ED]-[0-9]{2}-(.*?)-[0-9]{2}>");
Also it seems you haven't called matchers find()
or matches()
method which is required before you can call group()
.
String text = "sdsas<D-10-helloworld-84>kjvkjv";
Pattern pattern = Pattern.compile("<[ED]-[0-9]{2}-(.*?)-[0-9]{2}>");
Matcher matcher = pattern.matcher(noiseFrame);
if (matcher.find()) {
String newText = matcher.group(1);
System.out.println(newText);
}
Upvotes: 4