正则表达式匹配问号,重复或评论除外( - )

时间:2022-02-25 22:26:08

I would like to build a regular expression in C# to match question mark except repeated or commented. For example, if I have a string below

我想在C#中构建一个正则表达式来匹配问号,除了重复或评论。例如,如果我在下面有一个字符串

--???
??
asdlfkj --?
asldfjl -?
aslfldkf --?
aslfkvlv --??
?
-?
dklsafdlafjd = ?

, I want to match like below (between * character).

,我想匹配如下(*字符之间)。

--???
??
asdlfkj --?
asldfjl -*?*
aslfldkf --?
aslfkvlv --??
*?*
-*?*
dklsafdlafjd = *?*

I'm developing SQL binding method using 2 parameters.

我正在使用2个参数开发SQL绑定方法。

The first one is SQL, for example

例如,第一个是SQL

select * from atable where id = ?.

SQL can have comment so I want ignore them.

SQL可以有注释,所以我想忽略它们。

The second one is parameter for SQL as Array to match sequentially;

第二个是SQL的参数作为Array顺序匹配;

Does anyone have good idea for it?

有没有人对它有好主意?

1 个解决方案

#1


1  

If you can negate this regex it should work for you:

如果你可以否定这个正则表达式它应该适合你:

(\?{2,}|(?<=--)\?)

I don't know what language you're working in, but you should be able to filter by line. Apply this regex as a predicate and either negate it or use a exclude function.

我不知道你在用什么语言,但你应该能够按行过滤。将此正则表达式应用为谓词,并将其否定或使用排除函数。

I'll leave those implementation details up to you.

我将把这些实施细节留给您。

#1


1  

If you can negate this regex it should work for you:

如果你可以否定这个正则表达式它应该适合你:

(\?{2,}|(?<=--)\?)

I don't know what language you're working in, but you should be able to filter by line. Apply this regex as a predicate and either negate it or use a exclude function.

我不知道你在用什么语言,但你应该能够按行过滤。将此正则表达式应用为谓词,并将其否定或使用排除函数。

I'll leave those implementation details up to you.

我将把这些实施细节留给您。