任何引号之间的字符串的正则表达式

时间:2021-05-19 09:37:28

I want to write a regular expression for a string starts with quotation mark and ends with same mark. It consists of alpha numeric words (e.g. "PL", or 'CS', . . . ). I thought about [^"].*[^"] , but this is only work for "" these. i want output like input: "CS300" output: 1 tSTRING or input:'a' ouput: 1 tSTRING

我想为带有引号的字符串写一个正则表达式,并以相同的标记结束。它由字母数字字组成(例如“PL”或“CS”,......)。我想过[^“]。* [^”],但这只适用于“”这些。我想输出像输入:“CS300”输出:1 tSTRING或输入:'a'输出:1 tSTRING

Thanks

谢谢

my code is

我的代码是

%{
int linecounter=1;
%}
%%
\n linecounter++;
(['"])[^'"]*\1 printf("%d tSTRING \n", linecounter);
%%
main()
{
yylex();
}

2 个解决方案

#1


1  

Use negated character class and backreference:

使用否定字符类和反向引用:

(['"]).*?\1

Explanation:

说明:

(['"]) : matches a single or a double quote and keep it in group1
.*?    : matches what is between
\1     : backreference, same quote as in group 1

If your regex flavor doesn't support lazy quantifiers:

如果你的正则表达式味道不支持惰性量词:

(['"])[^'"]*\1

#2


0  

I write \"(\.|[^"])\" and \'(\.|[^'])\' now its working.

我现在写“\(\。| [^”])\“和\'(\。| [^'])\”。

#1


1  

Use negated character class and backreference:

使用否定字符类和反向引用:

(['"]).*?\1

Explanation:

说明:

(['"]) : matches a single or a double quote and keep it in group1
.*?    : matches what is between
\1     : backreference, same quote as in group 1

If your regex flavor doesn't support lazy quantifiers:

如果你的正则表达式味道不支持惰性量词:

(['"])[^'"]*\1

#2


0  

I write \"(\.|[^"])\" and \'(\.|[^'])\' now its working.

我现在写“\(\。| [^”])\“和\'(\。| [^'])\”。