匹配可选模式的正则表达式
Regular expression to match optional patterns
我知道 Regex 是一个非常热门的话题,并且有很多类似的问题,但是,我还没有找到符合我需求的问题。
我需要检查我的字符串的格式如下:
- 所有行必须以 5 位数字开头。
- 字符 6 到 12 必须是白色 space。
- 字符 13 必须是白色 space 或星号。
- 如果在最后一个句点之前有任何句点、冒号或分号,字符前面不能有白色space,但后面必须有白色space。
- 左括号后不能跟白色 space。
- 右括号前不能有白色 space。
我还没有尝试实现冒号、分号或括号,但到目前为止我只停留在句号上。这些字符是可选的,所以我无法进行硬检查,我正在尝试捕获它们,但在
这样的情况下我仍然得到匹配
00000 *TEST .FINAL STATEMENT. //Matches, but it shouldn't match.
00001 *TEST2 . FINAL STATEMENT. //Matches, but it shouldn't match.
00002 *TEST3. FINAL STATEMENT. //Matches, **should** match.
这是我目前使用的正则表达式:
^\d{5}\s{6}[\s\*][^.]*([^.\s]+\.\s)?[^.]*\..*$
我真的不明白这是怎么回事,特别是因为我使用 [^.] 来表示我将接受除句点以外的任何内容作为通配符,并且可选模式乍一看是正确的:如果有句点,它后面不应该有白色 space,后面应该有白色 space。
试试这个:
^\d{5}\s{6}[\s\*] # Your original pattern
(?: # Repeat 0 or more times:
[^.:;()]*| # Unconstrained characters
(?<!\s)[.:;](?=\s)| # Punctuation after non-space, followed by space
\((?!\s)| # Opening parentheses not followed by space
(?<!\s)\) # Closing parentheses not preceeded by space
)*
\.$ # Period, then end of string
https://regex101.com/r/WwpssV/1
pattern的最后部分,有特殊要求的字符是.:;()
,所以使用负数字符集来匹配除了那些字符之外的任何字符:[^.:;()]*
然后交替使用:
if there is any period, colon or semicolon before the final period, the character must not be preceded by a white space, but it must be followed by a white space.
Fulfilled by (?<!\s)[.:;](?=\s)
- 仅当前面没有 space 且后跟 space.
时才匹配这些字符之一
opening parentheses cannot be followed by a white space.
由 \((?!\s)
完成
closing parentheses cannot be preceded by a white space.
由 (?<!\s)\)
完成
然后在模式末尾交替使用这 4 种可能性。
我知道 Regex 是一个非常热门的话题,并且有很多类似的问题,但是,我还没有找到符合我需求的问题。
我需要检查我的字符串的格式如下:
- 所有行必须以 5 位数字开头。
- 字符 6 到 12 必须是白色 space。
- 字符 13 必须是白色 space 或星号。
- 如果在最后一个句点之前有任何句点、冒号或分号,字符前面不能有白色space,但后面必须有白色space。
- 左括号后不能跟白色 space。
- 右括号前不能有白色 space。
我还没有尝试实现冒号、分号或括号,但到目前为止我只停留在句号上。这些字符是可选的,所以我无法进行硬检查,我正在尝试捕获它们,但在
这样的情况下我仍然得到匹配00000 *TEST .FINAL STATEMENT. //Matches, but it shouldn't match.
00001 *TEST2 . FINAL STATEMENT. //Matches, but it shouldn't match.
00002 *TEST3. FINAL STATEMENT. //Matches, **should** match.
这是我目前使用的正则表达式:
^\d{5}\s{6}[\s\*][^.]*([^.\s]+\.\s)?[^.]*\..*$
我真的不明白这是怎么回事,特别是因为我使用 [^.] 来表示我将接受除句点以外的任何内容作为通配符,并且可选模式乍一看是正确的:如果有句点,它后面不应该有白色 space,后面应该有白色 space。
试试这个:
^\d{5}\s{6}[\s\*] # Your original pattern
(?: # Repeat 0 or more times:
[^.:;()]*| # Unconstrained characters
(?<!\s)[.:;](?=\s)| # Punctuation after non-space, followed by space
\((?!\s)| # Opening parentheses not followed by space
(?<!\s)\) # Closing parentheses not preceeded by space
)*
\.$ # Period, then end of string
https://regex101.com/r/WwpssV/1
pattern的最后部分,有特殊要求的字符是.:;()
,所以使用负数字符集来匹配除了那些字符之外的任何字符:[^.:;()]*
然后交替使用:
if there is any period, colon or semicolon before the final period, the character must not be preceded by a white space, but it must be followed by a white space.
Fulfilled by (?<!\s)[.:;](?=\s)
- 仅当前面没有 space 且后跟 space.
opening parentheses cannot be followed by a white space.
由 \((?!\s)
closing parentheses cannot be preceded by a white space.
由 (?<!\s)\)
然后在模式末尾交替使用这 4 种可能性。