I am trying to make regex for finding all patterns within the 20 first characters within a line.
This is my regex until now:
?:^[\w\W]{0,20})(\b[1-9]\d\s{0,1}\d{2}\s{0,1}\d{2}\s{0,1}\d{2}\b)
Original I had:
\b[1-9]\d\s{0,1}\d{2}\s{0,1}\d{2}\s{0,1}\d{2}\b
Which works fine, but I need a boundary for the search within first 20 characters.
I want to end up with following matches:
Text: 20202320
Match -> 20202320
Text: 30221130 30222120
Match -> 30221130, 30222120
Text: 20202220202020 34202202 skdjldfslkjfd sdlkjflkjsdflkjsdflksdjflskjdflskdjflsdkjf
Match -> 22020202, 34202202
Text (more than 20 before spaces): 34202202
Match nothing
Text: 2020222222244401 34202202
Match -> 22244401, 34202202
Text: sdlkjflkjsdflkjsdflksdjflskjdflskdjflsdkjfsdasdsa 34202202
Match nothing
Following regex would work in Java:
final Pattern p = Pattern.compile(
"(?<=^.{0,19})[1-9]\\d\\s?\\d{2}\\s?\\d{2}\\s?\\d{2}\\b");
Also modern Javascript allows dynamic length assertion in lookbehind so that can let us use this regex:
/(?<=^.{0,19})[1-9]\d\s?\d{2}\s?\d{2}\s?\d{2}\b/gm
(?<=^.{0,19})
is a positive lookbehind assertion that makes sure we have 0 to 19 characters behind current position thus starting our match within first 20 characters only.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.