关于正则表达式的新手问题 [英] newbie question about regular expressions
问题描述
嗨
我试图理解如何编写一个正则表达式,它将为我提供一个具有以下特征的模式:
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 thestring
and the
这篇关于关于正则表达式的新手问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!