How do I write a regex to match a string that doesn't contain a word? [duplicate]

不问归期 提交于 2019-12-02 17:07:39

问题


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.


回答1:


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 )



来源:https://stackoverflow.com/questions/3147995/how-do-i-write-a-regex-to-match-a-string-that-doesnt-contain-a-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!