这个正则表达式会做什么你问并捕捉你的文本匹配:
(^ *\*.*|;\*.*)
我们使用组构造来捕获一切,然后使用OR(|)传入两个正则表达式。
要打破下来,让我们先从之间的第一部分“(”和“|”:
^ = start at the beginning of a line
* = followed by zero or more spaces (note there's a [space] hiding in there)
\* = followed by an '*'
.* = followed by zero or more of any character (all the way to end of line)
为了表达之间的第二部分“|”和“)” :
;\* = look for ';*'
.* = followed by zero or more of any character (all the way to end of line)
我注意到的一件事是,你没有考虑';'之间的可能空间,和'*'。如果你需要的话,我们只需要在“零个或多个空格”片添加到表达式中的第二部分:用来测试这个
(^ *\*.*|; *\*.*) // note [space] characters hiding in there.
这里是“测试文件”:
*
* this is a comment
*
* this is a comment too
A = B*C;*comment starts from here, but not before C.
A = B*C; *comment starts from here, with a space for readability.
您可以在https://www.regex101.com(或根据您的偏好选择其他人)进行测试。
还有其他优化可能需要用硬编码的[空格]字符替代\s
元序列,但我试图完全按照您的要求进行操作。
好的,谢谢你让我们知道。你试过什么了?你遇到了什么具体问题?你的问题是什么?您不能只发布需求并提出广泛的帮助请求。 – tnw
尝试先使用这样的东西http://regexr.com/ – andrewdleach
到目前为止,我已经结合了“\\ * [^ \\ n \\ r] * +”和“\\![^ \\ n \\ r] * +“并通过http://www.regexplanet.com/advanced/java/index.html进行测试。 –