I am trying to use Regex to find out if a string matches *abc - in other word开发者_开发问答s, 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.
abc$
You need the $
to match the end of the string.
.*abc$
should do.
So you have a few "fish" here, but here's how to fish.
- An online expression library and .NET-based tester: RegEx Library
- An online Ruby-based tester (faster than the .NET one) Rubular
- A windows app for testing exressions (most fully-featured, but no zero-width look-aheads or behind) RegEx Coach
Try this instead:
.*abc$
The $ matches the end of the line.
^.*abc$
Will capture any line ending in abc.
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).
If you want a string of 4 characters ending in abc use, /^.abc$/
精彩评论