norman bates
norman bates

Reputation: 43

How can we write regular expression for strings with two periods

I'm looking to write a regex which accepts only ".wms" files.

My current regex is " /\.(wms)$/i "

I'm using this is Rails Model validation and I'm getting this error

The provided regular expression is using multiline anchors (^ or $)

How can we get a regex which all the below match and avoid the above rails error

Regex should allow :

Regex should not allow:

Which regex should i use to get this validation right?

Upvotes: 2

Views: 667

Answers (3)

Nimish Gupta
Nimish Gupta

Reputation: 3175

try this regex

/\w+\.wms$/

Hope this helps

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626748

You need to use \A to match the start of the string and \z anchor to match the end of the string. Besides, a period should be escaped to match a literal period. Besides, to match any 0+ chars as many as possible, you may use .*.

Thus, you may use

/\A.*\.wms\z/

See the Rubular demo. (The demo regex is modified to use other anchors since the input is a multiline string.)

Upvotes: 1

Dr. X
Dr. X

Reputation: 2920

You can use below regular expression

([A-Za-z.-_]+.wms)

If you check above regular expressions in this link https://regex101.com/ Result will be

Match 1
Full match  0-12    `filename.wms`
Group 1.    0-12    `filename.wms`
Match 2
Full match  14-30   `filename.zip.wms`
Group 1.    14-30   `filename.zip.wms`

Regards

Upvotes: 0

Related Questions