I'm trying to perform a search on a line for a specific word "seed" and then check that the number after the word ends in "00". The line looks like this
2022/05/13 17:32:26 10716828 118693c3 [DEBUG Client 8228] Generating level 13 area "1_1_town" with seed 120481576136500
I can get the regex for the word and the space after it with \bseed\b\s
I can also get the last two digits with a simple [0][0]$
(since the number is always at the and of the line.).
I can't seem to merge the two so it checks for the word seed and then matches on the "00" (the \d[0][0] doesn't work).
What regex expression do I need to get things working properly ?
CodePudding user response:
Since different languages handle regex differently, here is a Java based solution. The first two ways return true if the seed is there. The third way, captures the seed number.
The first matches any complete string that contains the requested criteria. Even though your seed was at the end of the line, this doesn't require that.
.*
- one or more of any character (here matches irrelevant characters)seed\\s
- seed followed by one or more white space characters\\d 00
- one or more digits(?!\\d)
- not followed by a digit
String s =
"2022/05/13 17:32:26 10716828 118693c3 [DEBUG Client 8228] Generating level 13 area \"1_1_town\" with seed 120481576136500";
String regex1 = ".*seed\\s \\d ?00(?!\\d).*";
System.out.println(s.matches(regex1));
prints
true
This is similar in that it returns true if it finds the pattern within the string. Thus the ending and trailing irrelevant characters (i.e. .*
) are not needed
String regex2 = "seed\\s \\d ?00(?!\\d)";
Matcher m = Pattern.compile(regex2).matcher(s);
System.out.println(m.find());
prints
true
If you want to retrieve the seed, do it like this by putting parens around the digits to form a capture group.
String regex2 = "seed\\s (\\d ?00)(?!\\d)";
Matcher m = Pattern.compile(regex2).matcher(s);
if (m.find()) {
System.out.println(m.group(1));
}
prints
120481576136500
CodePudding user response:
\bseed\s \d*00$
\bseed
matches the wordseed
\s
matches whitespace after it\d*
matches zero or more digits00$
matches00
at the end of the line.