Reputation: 7920
Given a string like:
ASSUME @pete, @grey and @matt_c ARE really tall
is there a way I can use regex to extract:
MATCH 1
1. `@pete`
2. `really tall`
MATCH 2
1. `@grey`
2. `really tall`
MATCH 3
1. `@matt_c`
2. `really tall`
Further, is there a way I can do it with the @ being optional for each of them?
Constraints: The syntax must be of the form ASSUME [names] ARE [statement] where:
,
(space)
, &
or and
Happy to answer any questions relating to setup. A starting point with the example strings I'm trying to make work can be found here: http://regex101.com/r/fS9oK5/4
Upvotes: 0
Views: 70
Reputation: 7948
a little variation from the accepted answer:
you would actually consume the first sub-pattern
(@[\w-]+)(?=.*ARE\s(.+))
to explicitly match ASSUME
, depending on your engine and \G
option
(?:^ASSUME\s*|\G[^@]*)(@[\w-]+)(?=.*ARE\s(.+))
Upvotes: 0
Reputation: 70732
You would need to use a Positive Lookahead to capture the overlapping matches.
(?=(@[\w-]+).*ARE\s*(.+))
Upvotes: 1
Reputation: 174816
I think you want something like this,
ASSUME (@\w+(?:(?:,?\s@\w+)*\s*and\s*@\w+)?)\sARE\s(.+)
Upvotes: 1