Alex Rivera | Logout

Regexp in ruby - can I use parenthesis without grouping?

Asked 2011-05-13T13:21:53.240
14

I have a regexp of the form:

/(something complex and boring)?(something complex and interesting)/

I'm interested in the contents of the second parenthesis; the first ones are there only to ensure a correct match (since the boring part might or might not be present but if it is, I'll match it by accident with the regexp for the interesting part).

So I can access the second match using $2. However, for uniformity with other regexps I'm using I want that somehow $1 will contain the contents of the second parethesis. Is it possible?

Edit
Report

1 Answer

2

Yup, use the ?: syntax:

/(?:something complex and boring)?(something complex and interesting)/
answered 2011-05-13T13:25:29.160

Your Answer