[英]RegEx match end character only when other character is not present
I'm having quite some trouble to define a regEx that I'm needing.... 我在定义我需要的regEx时遇到了很多麻烦。
Basically the idea is to detect all lines that end with a ,
or a ;
基本想法是,以检测所有的行为此用
,
或;
character. 字符。 For this I have defined the following regex:
为此,我定义了以下正则表达式:
(,|;)$
Which works fine for this, but then I have the exception that if there's a *
character within that line (not necessarily starting with, but at some position), then I don't want to detect that match. 对此很好用,但是我有一个例外,如果该行中有一个
*
字符(不一定以*
开头,而是在某个位置),那么我就不想检测到该匹配项。 Based on this sample: 基于此样本:
/**
* Here there's a comment I don't want to find,
* but after this comment I do
*/
detectMe;
other,
I would intend to find 2 groups, the first one 我打算找到2个小组,第一个
/**
* Here there's a comment I don't want to find,
* but after this comment I do
*/
detectMe;
And the second one 还有第二个
other,
I've tried many things such as non capturing groups, negative looks ahead and also start of a string with [^\\s*\\*]
with no success. 我尝试了很多事情,例如未捕获组,否定的前瞻以及以
[^\\s*\\*]
开头的字符串也没有成功。 Is there a way to do this? 有没有办法做到这一点?
Some of the regEx I've tried... 我尝试过的一些regEx ...
^[^\*](.*?)(,|;)$
^[^\s*\*](.*?)(,|;)$
To match an optional C comment and the following line ending with ;
匹配可选的C注释和以下以
;
结尾的行;
or ,
you may use 或
,
您可以使用
/(?:\/\*+[^*]*\*+(?:[^\/*][^*]*\*+)*\/\r?\n)?.*[;,]$/gm
See this regex demo 观看此正则表达式演示
Details 细节
(?:\\/\\*+[^*]*\\*+(?:[^\\/*][^*]*\\*+)*\\/\\r?\\n)?
- an optional (as there is a ?
quantifier after the group) non-capturing group matching 1 or 0 occurrences of ?
量词)
\\/\\*+[^*]*\\*+(?:[^\\/*][^*]*\\*+)*\\/
- a C comment pattern \\/\\*+[^*]*\\*+(?:[^\\/*][^*]*\\*+)*\\/
- C注释模式 \\r?\\n
- a CRLF or LF ending \\r?\\n
-CRLF或LF结尾 .*[;,]$
- a whole line that ends with ;
.*[;,]$
-以;
结尾的整行;
or ,
( $
is the end of a line anchor here due to m
modifier). ,
( $
是m
修饰符,此处是行锚的结尾)。 You can use this regex: 您可以使用此正则表达式:
/^[^*]*?[,;]$/gm
It will start by mathing any number of characters not being ' *
', then match ' ,
' or ' ;
它将首先对任意数量的非'
*
'字符进行数学运算,然后匹配' ,
'或' ;
' at the end of the line. 在行尾。 It uses the
global
and multiline
flags to match all lines. 它使用
global
和multiline
标志来匹配所有行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.