[英]regex pattern to match specific number pattern , skip if there different pattern
Requirement: 需求:
If pattern 57XXXXXXX OR 57XXXXXXX-X found in a sentence , then copy this matched pattern ( X - denotes 7 integer number and 57 are constant values must be there), else ignore complete sentence. 如果在一个句子中找到模式57XXXXXXX或57XXXXXXX-X ,则复制这个匹配的模式( X - 表示7个整数, 57个是常量值必须在那里),否则忽略完整的句子。
I have written a regex pattern 57[0-9]{7}|-[0-9]{1}
to do match both the pattern. 我写了一个正则表达式模式57[0-9]{7}|-[0-9]{1}
来匹配两个模式。
If below pattern found(8 digits after 57 instead 7 , then still above regex still gets the matching pattern (actually expecting regex to not match) 如果发现下面的模式(57之后的8位数字而不是7,则仍然在正则表达式之上仍会得到匹配的模式(实际上期望正则表达式不匹配)
for eg 5712345678-0 (after 57 , 8 digits in sentance) --> regex matches and gives 571234567-0 例如5712345678-0(在57后,情感中的8位数字)->正则表达式匹配并给出571234567-0
Using java to compile above pattern. 用java编译上面的模式。
You could try this: 你可以试试这个:
\b57\d{7}(?:-\d)?\b
Here's what it looks like: 这是它的样子:
In Java, that would be Pattern.compile("\\\\b57\\\\d{7}(?:-\\\\d)?\\\\b")
. 在Java中,这应该是Pattern.compile("\\\\b57\\\\d{7}(?:-\\\\d)?\\\\b")
。
差别不大但允许字母和下划线:
(?:(?<=[^0-9])|^)57[0-9]{7}(?:-[0-9])?(?:(?=[^0-9])|$)
You could use lookahead assertions in this case. 在这种情况下,您可以使用前瞻断言。
57\d{7}(?:-\d)?(?!\d)
Regular expression: 正则表达式:
57 '57'
\d{7} digits (0-9) (7 times)
(?: group, but do not capture:
- '-'
\d digits (0-9)
)? end of grouping
(?! look ahead to see if there is not:
\d digits (0-9)
) end of look-ahead
Or: 要么:
(?:57\d{7})(?:-\d)?(?!\d)
Regular expression: 正则表达式:
(?: group, but do not capture:
57 '57'
\d{7} digits (0-9) (7 times)
) end of grouping
(?: group, but do not capture
- '-'
\d digits (0-9)
)? end of grouping
(?! look ahead to see if there is not:
\d digits (0-9)
) end of look-ahead
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.