Wall
Wall

Reputation: 313

Eclipse regex matching for ending strings

In my code I have some strings as follows:

blabla AS myString, blabla

I'd like to capture AS myString, only, but the regex I'm using now captures everything after the comma. I already enabled the checkbox for regex in the Find/Replace window, so my regex is:

AS\s[^"]+(,)

Am I doing something wrong?

Upvotes: 1

Views: 179

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627126

The [^"]+(,) part matches any 1 or more chars other than ", as many as possible, up to the , char. That means all commas before the first " are grabbed, too.

You may modify the pattern to use

AS\s+[^",]+

See the regex demo.

To match AS as a whole word you may add a word boundary:

\bAS\s+[^",]+
^^

Details

  • \b - word boundary
  • AS - a literal string
  • \s+ - one or more whitespace chars
  • [^",]+ - one or more chars other than " and ,.

Upvotes: 2

Related Questions