RegEx search and replace in pattern

时间:2021-11-15 16:50:35

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:

我需要搜索一个可以从一个文档更改为文档但是遵循某种模式的模式。该模式将始终是9个数字,后跟3个字母。它有时会在它们之间留有空间,有时则没有。以下是要搜索的文本示例:

  1. 009244828 FLE
  2. MID021087275
  3. 006386476JJK
  4. 002973303 JJK
  5. MNS 000110924
  6. MNS000110924
  7. 009244828PSC
  8. 001915657SCR

My current regex looks like this: .+?(?=(JJK|FLE|PSC|SCR)). This returns lines 1,3,4,7 & 8 like this:1.

我目前的正则表达式如下:。+?(?=(JJK | FLE | PSC | SCR))。这将返回1,3,4,7和8行:1。

  1. 009244828\s
  2. 006386476
  3. 002973303\s
  4. 009244828
  5. 001915657

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:

因为它应该但不会返回字母。我需要用字母返回这些行,如果它在那里则删除空格。我的返回结果应如下所示:

  1. 009244828FLE
  2. 006386476JJK
  3. 002973303JJK
  4. 009244828PSC
  5. 001915657SCR

1 个解决方案

#1


0  

Let's compose the regex you're looking for step-by-step.

让我们一步一步地构建你正在寻找的正则表达式。

What you need to match is this:

你需要匹配的是:

  • 9 decimal digits \d{9}
  • 9位小数\ d {9}

  • An optional whitespace character \s?
  • 一个可选的空格字符?

  • 3 UPPERCASE letters [A-Z]{3} (use [a-zA-Z]{3} if the letters may be lowercase)
  • 3个大写字母[A-Z] {3}(如果字母可能是小写,请使用[a-zA-Z] {3}

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.

我说“差不多”因为它不会让你摆脱数字和字母之间的空间。为此,您需要将字母和数字放入捕获组中 - 之后,您可以简单地连接捕获的子字符串(使用$ 1 $ 2或\ 1 \ 2的替换表达式)来获得您想要的内容。

(\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})$

#1


0  

Let's compose the regex you're looking for step-by-step.

让我们一步一步地构建你正在寻找的正则表达式。

What you need to match is this:

你需要匹配的是:

  • 9 decimal digits \d{9}
  • 9位小数\ d {9}

  • An optional whitespace character \s?
  • 一个可选的空格字符?

  • 3 UPPERCASE letters [A-Z]{3} (use [a-zA-Z]{3} if the letters may be lowercase)
  • 3个大写字母[A-Z] {3}(如果字母可能是小写,请使用[a-zA-Z] {3}

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.

我说“差不多”因为它不会让你摆脱数字和字母之间的空间。为此,您需要将字母和数字放入捕获组中 - 之后,您可以简单地连接捕获的子字符串(使用$ 1 $ 2或\ 1 \ 2的替换表达式)来获得您想要的内容。

(\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})$