Reputation: 9903
I am trying to use Regex to find out if a string matches *abc - in other words, it starts with anything but finishes with "abc"?
What is the regex expression for this? I tried *abc but "Regex.Matches" returns true for xxabcd, which is not what I want.
Upvotes: 2
Views: 2878
Reputation: 15577
So you have a few "fish" here, but here's how to fish.
Upvotes: 4
Reputation: 2340
If you want a string of 4 characters ending in abc use, /^.abc$/
Upvotes: 0
Reputation: 9653
It depends on what exactly you're looking for. If you're trying to match whole lines, like:
a line with words and spacesabc
you could do:
^.*abc$
Where ^
matches the beginning of a line and $
the end.
But if you're matching words in a line, e.g.
trying to match thisabc and thisabc but not thisabcd
You will have to do something like:
\w*abc(?!\w)
This means, match any number of continuous characters, followed by abc and then anything but a character (e.g. whitespace or the end of the line).
Upvotes: 1