0

I need to extract a string after opResult and before another string (word+ '=')

For example:

testest=false opResult=Critical extension not supported random=abc srcPort=10

So I should extract out Critical extension not supported, before the next word with an equals sign.

Also, it should also work if there is no other string at the back, meaning I should get the same result with the below example.

typesOnly=false opResult=Critical extension not supported

The regular expression I have currently extracted everything before the last '=' sign.

opResult=(\S.*)(\s\w+=)

3 Answers 3

2

We can try matching/extracting using the following pattern:

.*opResult=(.*?)(?:\\s*\\S+=.*|$)

The trick here is in being able to articulate when the next key begins, and then to not extract it.

String line = "testest=false opResult=Critical extension not supported random=abc srcPort=10";
String pattern = ".*opResult=(.*?)(?:\\s*\\S+=.*|$)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
if (m.find( )) {
    System.out.println(m.group(1));
}

Output:

Critical extension not supported

Demo here:

Rextester

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

3 Comments

It only works if there are string at the back but it should also work if there is no string at the back.
I just updated to cover the case where opResult is the final key in the string. Other than this, I don't know what you mean.
Yes, that's what I wanted! Thank you!!
0

We need to find text after 'opResult=' before the next blankspace(\s)word(\w+) followed by an equals sign (?==) and whatever follows (.*)

Pattern becomes

.*opResult=([\w|\s]+)(\s\w+(?==).*)

Here's the link to Regex101

Comments

0

You should work with positive lookbehind and positive lookahead and a lazy quantifier for the text in between

(?<=opResult=).*?(?=\s*\S*=)

You can see the results on regex101.

2 Comments

it shouldn't grab the random word, should only get 'Critical extension not supported'
@Kaycee Didn't see it, but here you go.

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.