我想使用正则表达式捕获字符串中的所有内容,直到冒号后跟一个空格,:
或者逗号后跟一个空格,,
但是如果在字符串中可以找到带有空格的冒号,则仅使用逗号作为条件。换句话说,如果没有:
,我不想捕获任何东西。如果:
在字符串中,我要捕捉一切,直到它或向上,直到,
当,
谈到前:
。
我正在尝试
/(?:(?!: )[^])*/g
和
/(?:(?!: )[^])*/g
一些示例文本:
在这里我只想捕捉DeBary
:
DeBary, OH: Suddenly on Thursday, June 16, 2011 at the age of 78.
在这里我只想捕捉DeBary
:
DeBary: Suddenly on Thursday, June 16, 2011 at the age of 78.
在这里我什么也不想捕获:
Suddenly on Thursday, June 16, 2011 at the age of 78.
这可能有效。
# (?s)^(?=.*:[ ]).*?(?=[,:][ ])
(?s) # Dot-all modifier
^ # BOS
(?= .* : [ ] ) # Lookahead for colon then space
.*? # Consume minimal any chars up until
(?= [,:] [ ] ) # Lookahead, Comma or colon, then whitespace
或者,也可以不进行最终的前瞻。
# (?s)^(?=.*:[ ])[^,:]*
(?s) # Dot-all modifier
^ # BOS
(?= .* : [ ] ) # Lookahead for colon then space
[^,:]* # Consume non comma nor colon
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句