我想采用在另一个文本模式之前发生的文本模式.
例如,我有这样的文字:
code 4ab6-7b5
Another lorem ipsum
Random commentary.
code f6ee-304
Lorem ipsum text
Dummy text
code: ebf6-649
Other random text
id-x: 7662dd41-29b5-9646-a4bc-1f6e16e8095e
code: abcd-ebf
Random text
id-x: 7662dd41-29b5-9646-a4bc-1f6e16e8095e
Run Code Online (Sandbox Code Playgroud)
我想采取在第一次code出现之前发生的最后一次id-x(这意味着我想获取代码ebf6-649)
我怎么能用regexp做到这一点?
如果你的正则表达式支持前瞻,你可以使用这样的解决方案
^code:[ ]([0-9a-f-]+)(?:(?!^code:[ ])[\s\S])*id-x
Run Code Online (Sandbox Code Playgroud)
你可以在捕获号码中找到你的结果1.
它是如何工作的?
^code:[ ] # match "code: " at the beginning of a line, the square
# brackets are just to aid readability. I recommend always
# using them for literal spaces.
( # capturing group 1, your key
[0-9a-f-]+ # match one or more hex-digits or hyphens
) # end of group 1
(?: # start a non-capturing group; each "instance" of this group
# will match a single arbitrary character that does not start
# a new "code: " (hence this cannot go beyond the current
# block)
(?! # negative lookahead; this does not consume any characters,
# but causes the pattern to fail, if its subpattern could
# match here
^code:[ ] # match the beginning of a new block (i.e. "code: " at the
# beginning of another line
) # end of negative lookahead, if we've reached the beginning
# of a new block, this will cause the non-capturing group to
# fail. otherwise just ignore this.
[\s\S] # match one arbitrary character
)* # end of non-capturing group, repeat 0 or more times
id-x # match "id-x" literally
Run Code Online (Sandbox Code Playgroud)
这个(?:(?!stopword)[\s\S])*模式让你尽可能地匹配,而不会超越另一个事件stopword.
请注意,您可能必须使用某种形式的多行模式^来匹配行的开头.该^避免假阴性,如果你是很重要的random text包含open:.
工作演示(使用Ruby的正则表达式,因为我不确定你最终将使用哪一个)
| 归档时间: |
|
| 查看次数: |
3637 次 |
| 最近记录: |