toosweetnitemare
toosweetnitemare

Reputation: 2276

does this regex mean what i think it means?

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

Upvotes: 2

Views: 178

Answers (5)

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$

Upvotes: 10

Haseena
Haseena

Reputation: 53

start_ then 3 alphanumeric character and then _ then allow 3 alphanumeric character followed by the .txt formate file name

Upvotes: -2

Big Ed
Big Ed

Reputation: 1244

I would say that you are close, but not entirely correct.

[a-z0-9]{3,}

will match 3 or more lower-case letters or digits. If your regex is run in a case-insensitive text it will also match upper-case letters.

As written, your regex will match any string that contains the pattern. If you wanted it to match the entire string, you would use markers for the beginning and end of the string:

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

Also, you probably don't need to escape the underscore.

Upvotes: 3

Martijn
Martijn

Reputation: 16103

This is a very usefull tool https://addons.mozilla.org/nl/firefox/addon/rext/

That way you can test it yourself. If programming in weblanguages, you would have to refresh every change, this updates on the fly, so you can tweak your regex very fast :)

Upvotes: 1

lonesomeday
lonesomeday

Reputation: 237845

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.

Upvotes: 5

Related Questions