K2xL
K2xL

Reputation: 10290

Match Options in Regular Expressions - But don't actually return match

Using rubular.com as a guide, I am trying to get this expression to work correctly.

^((move|travel|fly) (.+) pixels)

Basically, this matches

move 5 pixels

fly 10 pixels

travel 3 pixels

The matches I get are:

  1. move 5 pixels

  2. move

  3. 5

The problem is that I don't want to return the matches for the options. Is there some way to match the regular expression but not return the match for those in that option? Ideally it would be:

  1. move 5 pixels

  2. 5

Thanks in advanced!

Upvotes: 1

Views: 70

Answers (2)

user1549550
user1549550

Reputation:

Use the ?: option on the group to stop it from being stored and used as a backreference.

^((?:move|travel|fly) (.+) pixels)

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838256

Use a non-capturing group:

^((?:move|travel|fly) (.+) pixels)

See it working online: rubular

Upvotes: 2

Related Questions