试图匹配“”中的字符串

时间:2022-08-14 18:05:24
"[a-zA-Z0-9]*"

which I wish to match the pattern of the form

我希望与表格的模式相匹配

"testing" 

But my regex is not matching any of these strings. Where I'm making the mistake?

但是我的正则表达式不匹配任何这些字符串。我在哪里弄错了?

Thanks in advance.

提前致谢。

3 个解决方案

#1


1  

if your requirement is "matching" not "extracting":

如果您的要求是“匹配”而不是“提取”:

^"[a-zA-Z0-9"]*"$ would be the regex you need.

note that since your 2nd example has nested double quotes, you need " in your []

请注意,由于您的第二个示例嵌套了双引号,因此您需要“在您的[]中

test with grep:

用grep测试:

kent$  echo '"testing"
"testing123"hello""
'|grep -E '^"[a-zA-Z0-9"]*"$'
"testing"
"testing123"hello""

if you want to extract things between double quotes, you need to explain, what do you want to get in your 2nd example.

如果你想在双引号之间提取东西,你需要解释一下,你想在第二个例子中得到什么。

EDIT

编辑

if you just want to extract the things between double quote:

如果你只是想在双引号之间提取东西:

(?<=")[^"]* 

is what you are looking for.

是你在找什么。

still testing with grep:

还在用grep测试:

kent$  echo '"xxx bbb _ foo bar"'|grep -Po '(?<=")[^"]*'
xxx bbb _ foo bar

#2


1  

I think you may have to escape the

我想你可能不得不逃避

"

with a

用一个

\

as in:

如:

\"[a-zA-z0-9]*\"

#3


1  

This will give you three matches as expected.

这将按预期为您提供三场比赛。

See this http://rubular.com/r/WW9IKTb0Xa

请参阅http://rubular.com/r/WW9IKTb0Xa

#1


1  

if your requirement is "matching" not "extracting":

如果您的要求是“匹配”而不是“提取”:

^"[a-zA-Z0-9"]*"$ would be the regex you need.

note that since your 2nd example has nested double quotes, you need " in your []

请注意,由于您的第二个示例嵌套了双引号,因此您需要“在您的[]中

test with grep:

用grep测试:

kent$  echo '"testing"
"testing123"hello""
'|grep -E '^"[a-zA-Z0-9"]*"$'
"testing"
"testing123"hello""

if you want to extract things between double quotes, you need to explain, what do you want to get in your 2nd example.

如果你想在双引号之间提取东西,你需要解释一下,你想在第二个例子中得到什么。

EDIT

编辑

if you just want to extract the things between double quote:

如果你只是想在双引号之间提取东西:

(?<=")[^"]* 

is what you are looking for.

是你在找什么。

still testing with grep:

还在用grep测试:

kent$  echo '"xxx bbb _ foo bar"'|grep -Po '(?<=")[^"]*'
xxx bbb _ foo bar

#2


1  

I think you may have to escape the

我想你可能不得不逃避

"

with a

用一个

\

as in:

如:

\"[a-zA-z0-9]*\"

#3


1  

This will give you three matches as expected.

这将按预期为您提供三场比赛。

See this http://rubular.com/r/WW9IKTb0Xa

请参阅http://rubular.com/r/WW9IKTb0Xa