我有两种可能的解决方案。两种解决方案都要求字符串长度为 8 个字符并以 S 开头。
鉴于此示例文本:
the problem is it's not the words that do not contain any words that I don't want
but actually any string that isn't a string that starts with an S and is 8 character long.
Example: S294KS12 this is the type of string I want on the document. Contains 8 characters
that are either digits or letters and starts with an S
SOMETIME
S294KS12
S1234567
S123456A
选项 1
此解决方案仅查找长度为 8 个字符且以 S 开头的字符串。
\bS[A-Z0-9]{7}\b
现场演示
https://regex101.com/r/lK0aO9/1
样本匹配
S294KS12
SOMETIME
S294KS12
S1234567
S123456A
说明
NODE EXPLANATION
----------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
----------------------------------------------------------------------
S 'S'
----------------------------------------------------------------------
[A-Z0-9]{7} any character of: 'A' to 'Z', '0' to '9'
(7 times)
----------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
----------------------------------------------------------------------
选项 2
此解决方案会进行额外检查以确保至少有一个额外的字母和一个数字。
\bS(?=[A-Z]*[0-9])(?=[0-9]*[A-Z])[A-Z0-9]{7}\b
现场演示
https://regex101.com/r/vH4lX2/3
样本匹配
S294KS12
S294KS12
S123456A
说明
NODE EXPLANATION
----------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
----------------------------------------------------------------------
S 'S'
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
[A-Z]* any character of: 'A' to 'Z' (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
[0-9] any character of: '0' to '9'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
[0-9]* any character of: '0' to '9' (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
[A-Z] any character of: 'A' to 'Z'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
[A-Z0-9]{7} any character of: 'A' to 'Z', '0' to '9'
(7 times)
----------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
----------------------------------------------------------------------
把所有的放在一起
要替换其他所有内容,我会将正则表达式合并到 ( ... )\s?|. 中,这将匹配所有内容,包括所需的字符串。
如果您随后在 Notepad++ 的替换为选项中使用 $1,那么您将只剩下所需的字符串。
我建议使用上面的选项 2,并将其插入到表达式中,使其看起来像这样:
(\bS(?=[A-Z]*[0-9])(?=[0-9]*[A-Z])[A-Z0-9]{7}\b)\s?|.
替换为: $1
现场演示
https://regex101.com/r/gO7zV7/1