I need to search out a pattern that can change from document to document but follows a certain pattern. The pattern will always be 9 numbers followed by 3 letters. It will sometimes have a space between them and sometimes not. Here is an example of text to search through:
My current regex looks like this: .+?(?=(JJK|FLE|PSC|SCR))
. This returns lines 1,3,4,7 & 8 like this:1.
as it should but does not return the letters. I need to return these lines with the letters and remove the space if it is there. my returned result should look like this:
Let's compose the regex you're looking for step-by-step.
What you need to match is this:
\\d{9}
\\s?
[AZ]{3}
(use [a-zA-Z]{3}
if the letters may be lowercase) Putting it all together, this regex does almost what you want:
\d{9}\s?[A-Z]{3}
I said "almost" because it doesn't let you get rid of the space between the digits and the letters. To do that, you need to put the letters and the digits into capturing groups - after that, you can simply concatenate the captured substrings (with a replacing expression along the lines of $1$2
or \\1\\2
) to get exactly what you want.
(\d{9})\s?([A-Z]{3})
If you want to make sure each digit-and-letter group is on its own line, simply wrap the entire regex in ^
and $
, then run it in a mode where these two characters match the beginning/end of a line instead of the beginning/end of the entire string.
^(\d{9})\s?([A-Z]{3})$
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.