1

I need to extract a word from a given string. The String might come in different ways in this case. e.g:

"The Signature refers to test id 69043 abcd. The Signature refers to test id 1001"

"The Signature refers to test id

69043 defg. The Signature refers to test id 1001"

"The Signature refers to test

id 69043."

Also test id might not be in lower case always. it would be better if i could ignore whether it is lower case or upper case. it might come as Test ID, TEST ID as well

I wrote this for the moment ' test id ([0-9]+) '

i want to extract 'test id number' from these given strings. sometimes it can have multiple 'test id number' in the string. some time string has multi lines because it comes in a paragraph.

1

1 Answer 1

5

You can use a formal Java pattern matcher here, using the following case insensitive pattern:

(?i)test\s+id\s+(\d+)

Consider the following code snippet:

String input = "The Signature refers to test id 69043 abcd. ";
input += "The Signature refers to test id 1001";
String pattern = "(?i)test\\s+id\\s+(\\d+)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);

while (m.find()) {
   System.out.println("Found value: " + m.group(1) );
}

This correctly outputs the two ids:

Found value: 69043
Found value: 1001

Demo

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

7 Comments

what if there are new lines between text and the digit
@MadushanChathuranga I just added a bunch of newlines to the demo and it still works. The only issue would be if you wanted to match something across newlines, but that doesn't appear to be needed here.
I want to match across new lines. value might be split in too new lines in some cases and it might not be ''test id '' always might as well 'Test ID', 'TEST ID' too. thank you
@MadushanChathuranga I made the pattern case insensitive, and it can now match the target pattern even if split among multiple lines.
\s allow different kinds of blank characters including EOL
|

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.