[英]Regex to match exact phrase before certain word
I would like to match any four digits occurring before double space and word "from" ex. 我想匹配出现在双倍空格和单词“ from”前的任何四个数字。
2356 from
1524 from
1223 from
The expression \\d{4} from
extracts digits, double space and the word "from", but ideally I would like to get only those four digits (without the trailing spaces and word "from") and only in cases when they are not part of number with hyphen like this example: \\d{4} from
表达式提取数字,双倍空格和单词“ from”,但理想情况下,我只想获取这四个数字(不带尾随空格和单词“ from”),并且仅当它们不是带有连字符的数字部分,例如以下示例:
2536 from
= 2536
2536 from
= 2536
8574 from
= 8574
8574 from
= 8574
35-6655 from
= no match 35-6655 from
=没有匹配项 3652
= no match (number without word "from" after the digits) 3652
=不匹配(数字后面没有单词“ from”的数字) You need to use either grouping (...)
operators in your regex: 您需要在正则表达式中使用任一分组
(...)
运算符:
(\d{4}) from
and then extract group #1 from the match. 然后从比赛中提取第1组。 Or a zero-width look-ahead match using
(?=...)
. 或使用
(?=...)
进行零宽度的超前匹配。
\d{4}(?= from)
To avoiding a match following the hyphen, you can use a zero-width negative look-behind. 为了避免在连字符后出现匹配,您可以使用零宽度负向后搜索。
(?<!-)\d{4}(?= from)
Explanation: 说明:
(?<!_)_______________
negative zero-width look-behind ... (?<!_)_______________
负零宽度后向...
____-________________
... doesn't match if a "-"
is found ____-________________
...如果找到"-"
,则不匹配
______\\d{4}__________
four digits ______\\d{4}__________
四位数
___________(?=______)
positive zero-width look-ahead ... ___________(?=______)
正零宽度预读...
______________ from_
... matches if " from"
is found ______________ from_
...如果找到" from"
则匹配
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.