如果您正则表达式的味道支持lookaheads,你可以使用这样
^code:[ ]([0-9a-f-]+)(?:(?!^code:[ ])[\s\S])*id-x
的解决方案,你可以找到你的结果在捕获数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
的(?:(?!stopword)[\s\S])*
模式让我们你尽可能的匹配,而不超出的stopword
另一个发生。
请注意,对于^
,您可能必须使用某种形式的多行模式才能匹配行首。如果您的random text
包含open:
,则^
对于避免错误否定很重要。
Working demo(使用Ruby的正则表达式的味道,因为我不知道哪一个,你最终要使用)
我使用Git的bash,所以我认为这是UNIX引擎 –
通过“用正则表达式”,我认为你的意思是“用git-bash”? (我的意思是,为什么你关心,如果答案恰巧使用正则表达式?) – ruakh
@JasonSwartz我实际上认为这个问题的以前版本是完全正确的,并会给你更有用的答案。这种有限形式的解决方案可能会在您的实际投入中产生误报。 –