I have the following regular expression in python:

pattern1 = re.compile('[a-zA-z]{1}[azA-z0-9]*["."][azA-z0-9]*\-[azA-z0-9]*$') 

How can I make it fit only one minus sign? Like abc123.abc123-abc123 , but not abc123.abc123----abc123 .

The method I use seems to work with a dot, but not with a minus.

  • So you remove these hyphens from character classes. ["."] must be replaced by \. - Wiktor Stribiżew
  • Do not use regular expressions where everything can be solved with a simple parser. - 0andriy

3 answers 3

You probably want this regular expression:

 [a-zA-z][a-zA-z0-9]*\.[a-zA-z0-9]+-[a-zA-z0-9]+$ 

Parsing:

 [a-zA-z] 

The first match is in English ( az lowercase, AZ uppercase).


 [a-zA-z0-9]* 

Then there can be any number ( * means from 0 to infinite) of English letters ( a-zA-Z ) or numbers ( 0-9 )


 \. 

Then you need to be a point (literally).


 [a-zA-z0-9]+ 

Then again, any number of English letters or numbers, but now at least one of these signs ( + means from 1 to infinite).


 - 

Literally this minus sign ( - )


 [a-zA-z0-9]+ 

This is already known (see above).


 $ 

Then there can be nothing else ( $ means the end of the line).


Notes:

  1. You may also want to start at the beginning of the line:

    ^[a-zA-z][a-zA-z0-9]*\.[a-zA-z0-9]+-[a-zA-z0-9]+$

    ( ^ means c start).

  2. It is safer to use r before a regular expression:

    pattern1 = re.compile(r'[a-zA-z][a-zA-z0-9]*\.[a-zA-z0-9]+-[a-zA-z0-9]+$')

  • A dot means any character. Probably you meant \. . - Qwertiy
  • one
    @Qwertiy spazibo, I corrected my answer. - MarianD

You just need to remove the hyphens)) If you don’t succeed, then as Wiktor Stribizew said to replace \ It's simple

     ["."] 

    This is something wrong.

     [azA-z0-9] 
     [a-zA-z0-9]