user380719
user380719

Reputation: 9903

What is the regex for *abc?

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

Answers (7)

Brad
Brad

Reputation: 15577

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

Upvotes: 4

David Harris
David Harris

Reputation: 2340

If you want a string of 4 characters ending in abc use, /^.abc$/

Upvotes: 0

steinar
steinar

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

leppie
leppie

Reputation: 117250

^.*abc$

Will capture any line ending in abc.

Upvotes: 1

riwalk
riwalk

Reputation: 14223

Try this instead:

.*abc$

The $ matches the end of the line.

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336198

.*abc$

should do.

Upvotes: 4

CanSpice
CanSpice

Reputation: 35808

abc$

You need the $ to match the end of the string.

Upvotes: 8

Related Questions