开发者

What is the regex for *abc?

开发者 https://www.devze.com 2023-01-21 06:55 出处:网络
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\"?

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$/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号