Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does this regex mean what i think it means?

Tags:

regex

Regex:

start\_[a-z0-9]{3,}\_[a-z0-9]{3,}\.txt

what i think it means:

  1. match on any string that begins with "start_"
  2. then has alphanumeric substring greater than 3 characters
  3. then separated with an underscore
  4. then has alphanumeric substring greater than 3 characters
  5. finally has a ".txt" extension

question:

can anyone confirm this behavior? I am able to verify pretty much everything via good except for what "{3,}" means. Any help is greatly appreciated!

-tsnm

like image 508
toosweetnitemare Avatar asked Jan 21 '26 18:01

toosweetnitemare


2 Answers

A few comments -

  1. start\_ should be ^start\_. That way you are assured its the start of the string (and not potentially the middle)
  2. [a-z0-9]{3,} is any lowercase alphanumeric character. If you want uppercase also you should make it [a-zA-Z0-9]. Also if you want it to be greater than 3 (and not equal to) make it {4,}
  3. This is good
  4. Same problems as 2
  5. If you want to make sure the .txt is at the end you should make it \.txt$.

Without my suggestions, this would match -

blahblahlbahstart_abc123_abc123.txtblahblahblah

And this would not -

start_ABC123_ABC123.txt

Also, '_' is not a special character for regexes. This means it should not be escaped by a \. So your final regex should be -

^start_[a-zA-Z0-9]{4,}_[a-zA-Z0-9]{4,}\.txt$

like image 133
David says Reinstate Monica Avatar answered Jan 23 '26 20:01

David says Reinstate Monica


You're very close. Let's take this in turn, as you did:

start\_

match on any string that begins with "start_"

Correct.

[a-z0-9]{3,}

then has alphanumeric substring greater than 3 characters

Close. It means "has alphanumeric substring 3 characters or more".

\_

then separated with an underscore

Correct.

[a-z0-9]{3,}

then has alphanumeric substring greater than 3 characters

Again, close. It means "has alphanumeric substring 3 characters or more".

\.txt

finally has a ".txt" extension

Correct.

like image 44
lonesomeday Avatar answered Jan 23 '26 19:01

lonesomeday



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!