[英]Match everything up to a specific character whether it exists or not
How do I match everything on the line but stop when I find a semicolon? 如何匹配线路上的所有内容,但在找到分号时停止? If I never find one I should continue matching even if one doesn't exist. 如果我找不到一个我应该继续匹配,即使一个不存在。
Here is some sample input: 以下是一些示例输入:
captureme; dontcaptureme
captureme andme andme
captureme andme andme; butnotme
I've tried a lazy quantifier (\\w*?);
我试过一个懒惰的量词(\\w*?);
but it won't work if I make the semicolon optional. 但如果我使分号可选,它将无法工作。 See my current regex fiddle here. 在这里查看我当前的正则表达式 。
You can use 您可以使用
^[^;\n]*
With multiline modifier. 使用多行修改器。
The ^
in a multiline mode will match at the beginning of a line, and the negated character class [^;\\n]
will match any character but a semi-colon or a newline symbol. 多行模式中的^
将在行的开头匹配,否定的字符类[^;\\n]
将匹配除分号或换行符号之外的任何字符。 *
will make the regex engine match those characters 0 or more times. *
将使正则表达式引擎匹配这些字符0次或更多次。 Thus, if you do not want to match emoty lines, use the +
quantifier instead. 因此,如果您不想匹配情绪线,请改用+
量词。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.