Regular expression, How to allow combination of dot (period) and letters?

前端 未结 7 589
庸人自扰
庸人自扰 2020-12-31 08:30

I want to allow (.) and (a-zA-Z) letters and _ and - , I have some problems with the (.) ,

Any idea ?

Thanks in advance ,

Ish

相关标签:
7条回答
  • 2020-12-31 09:05

    As everyone already said, if you enclose a set of characters (no need to escape in this situation) in square brackets, you are saying: "please allow these characters I'm putting inside. I found a reference video for you: Skip to 22-23 min

    0 讨论(0)
  • 2020-12-31 09:13

    This will do [a-zA-Z_.-]+

    Outside the character class, ([]), you need to escape the dot (\.)as it is a meta character.

    [a-z]+\.com  #matches `something.com`
    
    0 讨论(0)
  • 2020-12-31 09:15
    [A-Za-z_.-]
    

    is a character class that includes all the characters you mentioned. Inside a character class, it's not necessary to escape the ., and you can avoid escaping the - if you put it first or last.

    If numbers are ok, too, you can shorten this to

    [\w.-]
    
    0 讨论(0)
  • 2020-12-31 09:17

    . Has a special meaning in regular expressions, it uses to denote any character. Therefore you need to use escape character.

    So you need to use \.

    0 讨论(0)
  • 2020-12-31 09:18

    This should work just fine:

      [A-z._\-]+
    

    Please be aware that you my have to escape that slash depending on your programming language.

    0 讨论(0)
  • 2020-12-31 09:21

    Escape it, as it's a special character:

    \.
    
    0 讨论(0)
提交回复
热议问题