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.
我有各种包含不同信息的字符串,但它们都有一个共同特征:它们包含请求者的用户名,后面有括号。类似的东西:"... paul [55] ..."我正在尝试编写一个正则表达式,它能够在我之前提取单词[dd] 这是我到目前为止所拥有的:"/(?P<user>\w+)\s\[\d\d\]/"但这仅在字符串以用户开头时才有效,例如"paul [55] has logged in"但它在案例,"user paul [55] has logged off" 我错过了什么?
"... paul [55] ..."
[dd]
"/(?P<user>\w+)\s\[\d\d\]/"
"paul [55] has logged in"
"user paul [55] has logged off"
尝试使用积极的前瞻断言:
(\w+)(?=\s?\[\d\d\])
仅当此表达式后跟可选空格和方括号中的两位十进制数时,此表达式才匹配一个或多个单词字符。