Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有以下正则表达式模式:
pattern = @"(?<=\[)[\da-f]{8}(?=\])";
它正确地从“ [f000000f] Bar ”中提取“ f000000f ” ,但它不应该与“ Foo [f000000f] Bar ”匹配,因此我添加了以确定模式的开头:^
^
pattern = @"^(?<=\[)[\da-f]{8}(?=\])";
但是这种模式不适用于两种输入。
谁能告诉我如何强制第一个模式仅在输入字符串的开头匹配?
提前致谢。
您需要在lookbehind内移动锚点,如下所示:
(?<=^[)[\da-f]{8}(?=])"
否则,您将不会 match [f000000f],因为您的表达式的任何部分都不会“消耗”左方括号[。
[f000000f]
[