David Eisen
David Eisen

Reputation: 281

Regex to get rid of a Capture Group

I am trying to get rid of capture group 1, but I am cannot figure out how.

See: http://regex101.com/r/qY6eA9

Example: AFM_LU78_8-14-08.pdf

Regex: (LU|Local|Lodge|Council|LL)(\d{1,6})

Result: LU78

Intended Result: 78

Any suggestions on what I am missing? Also is there any kind of codeacademy type tutorial for regex?

Upvotes: 2

Views: 69

Answers (1)

Chris
Chris

Reputation: 136967

Use a non-capturing group:

(?:LU|Local|Lodge|Council|LL)(\d{1,6})

From the documentation:

The fact that plain parentheses fulfill two functions is not always helpful. There are often times when a grouping subpattern is required without a capturing requirement. If an opening parenthesis is followed by "?:", the subpattern does not do any capturing, and is not counted when computing the number of any subsequent capturing subpatterns. For example, if the string "the white queen" is matched against the pattern the ((?:red|white) (king|queen)) the captured substrings are "white queen" and "queen", and are numbered 1 and 2. The maximum number of captured substrings is 99, and the maximum number of all subpatterns, both capturing and non-capturing, is 200.

Upvotes: 1

Related Questions