-1

I'm very new to Regular expressions. I have a text "cat/1.39 bla, dog" I know that ^ is used to match a string that begins with something and $ is used to match something that ends with something.

This is what I could write -

cat/[^,\s;] --> matches "cat/1.39 bla, monkey" But "cat/[^,\s;].*dog$" doesn't match a string that begins with a cat and ends with a dog

Thanks for any help.

user1799214
  • 511
  • 2
  • 11
  • 25

2 Answers2

0

^cat..*dog$

This should work for you. You can test it out here.

Also, check this answer for a sloution using word boundaries.

Srini
  • 1,626
  • 2
  • 15
  • 25
0

cat/[^,\s;].*dog$

Note that whitespace after the word "dog" will cause the regex to fail. So if your string was "cat/1.39 bla, dog " your regex (and the "^cat.*dog$" that others have posted will fail.

Also remember that the "^" character inside and at the front of the brackets [] means to match anything but what is inside your brackets []. Thus [^,\s;] will stop at any of those characters. So if you have a string like "cat/1.39 bla, dog", the regex will proceed all the way to the "," character and stop. In your case the ".*" should still allow you to continue until you get to "dog".

Also

You can test regex here. Note that the first line doesn't match because I have added to space characters after the word dog.

Doug B
  • 180
  • 3
  • 10