Reputation: 2276
Regex:
start\_[a-z0-9]{3,}\_[a-z0-9]{3,}\.txt
what i think it means:
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
Reputation: 20129
A few comments -
start\_
should be ^start\_
. That way you are assured its the
start of the string (and not potentially the middle)[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,}
\.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
Reputation: 53
start_ then 3 alphanumeric character and then _ then allow 3 alphanumeric character followed by the .txt formate file name
Upvotes: -2
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
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
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