关于正则表达式的新手问题 [英] newbie question about regular expressions

查看:76
本文介绍了关于正则表达式的新手问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我试图理解如何编写一个正则表达式,它将为我提供一个具有以下特征的模式:



1.特定字符 - 我猜测使用方括号[]

2.只有其中一个(不是组合或全部) - 我猜测使用OR|

3.所选字符为零或一 - 我猜是使用问号?



我不知道如何将这些结合起来以获得我想要的东西

我试过这样的东西但它不起作用

[c?| b ?]



任何帮助都会很棒。

谢谢



ps

好​​结果将是:

b,c,

糟糕的结果:

其他一切

Hi
I've tried to understand how to write a regex that will match for me a pattern with the following characteristics:

1. specific characters - I'm guessing the use of square braces "[]"
2. only one of them (not combination or all) - I'm guessing the use of OR "|"
3. zero or one for the selected character - I'm guessing the use of question mark "?"

I don't know how to combine these in order to get what I want
I tried something like this but it doesn't work
"[c?|b?]"

Any help will be great.
Thanks

p.s.
good results will be:
"b","c",""
bad result:
everything else

推荐答案

您好,



在一个角色类中(之间) [] ),只有 - (破折号)是一个特殊字符; | 将被视为正常字符。

试试这个:

Hi,

In a character class (between [ and ] ), only - (the dash) is a special character; ? and | would be treated as 'normal' characters.
Try this:
^(c|b)?




关于 ^ 的说明


Explanation about the ^ and


签名:

^ 符号提到<$ c的开头$ c> string
sign:
the ^ sign mentions the start of the string and the


这篇关于关于正则表达式的新手问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆