1

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

2 Answers 2

4

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().

Code:

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);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Removed anchors but still no match found.
check updated now. It seems you have't called find() or matches() method yet.
in this case all that is happening is newText is being set to "helloworld" without the other parts left and right of it :/
It is because you have brackets around inner most part only (.*?) if you want full match you can just use: String newText = matcher.group(0);
The point is not to get the message inside but to get the frame itself all of "<D-10-helloworld-84>" which may appear in a string such as sdsas<D-10-helloworld-84>kjvkjv
|
0

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"
}

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.