2

I have a String :

<E-25-Lorem Ipsum is simply dummy text-34>

I want to get the string "Lorem Ipsum is simply dummy text" and assign it to a variable "msg" so that I can use it else where.

Here is my code :

    String pattern = "([A-Za-z_ ])\\w+";
    //Create a Pattern object
    Pattern r = Pattern.compile(pattern);

    //Matcher Object
    Matcher m = r.matcher(frame);
    String msg = new String();

    if (m.find( )){
        msg = m.group(0);
    }

System.out.println(msg);

System.out.println(msg) on prints out the "Lorem", instead it is meant to print "Lorem Ipsum is simply dummy text".

1 Answer 1

3

You just need to apply a + quantifier to [A-Za-z_ ] character class to match 1 or more occurrences:

([A-Za-z_ ]+)\w+
           ^

See regex demo

Depending on whether you need to capture the first letter or not, you can also consider using

([A-Za-z_ ])\w+(?:\s+\w+)*

See another regex demo

Sign up to request clarification or add additional context in comments.

2 Comments

Just do not forget to use double backslashes when using these regexps in Java code.
Does it work for you? Please consider accepting the answer if it does.

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.