如何编写正则表达式来匹配不包含单词的字符串?
可能的重复:
正则表达式匹配不包含单词的字符串?
为了不匹配一组字符,我会使用例如 [^\"\\r\\n]*
现在我想不匹配固定字符集,例如“|=”
换句话说,我想匹配:(不是 "、不是 \r、不是 \n、不是 |= )。
编辑:我正在尝试修改正则表达式来解析用分隔符分隔的数据。我从 a CSV 解析器 获得的单分隔符解决方案,但现在我想要将其扩展为包含多字符分隔符。我认为前瞻不起作用,因为我想使用匹配的字符,而不仅仅是断言和丢弃。
Possible Duplicate:
Regular expression to match string not containing a word?
To not match a set of characters I would use e.g. [^\"\\r\\n]*
Now I want to not match a fixed character set, e.g. "|="
In other words, I want to match: ( not ", not \r, not \n, and not |= ).
EDIT: I am trying to modify the regex for parsing data separated with delimiters. The single-delimiter solution I got form a CSV parser, but now I want to expand it to include multi-character delimiters. I do not think lookaheads will work, because I want to consume, not just assert and discard, the matching characters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现,它应该是:
((?![\"\\r\\n]|[|][=]).)*
完整的正则表达式,从 CSV 解析器修改原始帖子中的链接将是:
((?((?![\"\\r\\n]|[|][=]).)*)|\"(? <字段>([^\"]|\"\")*)\")([|][=]|(?\\r\\n|\\n|$))< /code>
这将匹配任意数量的字符(不是 "、不是 \r、不是 \n 和不是 |= )或带引号的字符串,后跟( "|=" 或行尾)
I figured it out, it should be:
((?![\"\\r\\n]|[|][=]).)*
The full regex, modified from the CSV parser link in the original post, will be:
((?<field>((?![\"\\r\\n]|[|][=]).)*)|\"(?<field>([^\"]|\"\")*)\")([|][=]|(?<rowbreak>\\r\\n|\\n|$))
This will match any amount of characters of ( not ", not \r, not \n, and not |= ), or a quoted string, followed by ( "|=" or end of line )