JSB
JSB

Reputation: 360

Matching regex in cases where a non-greedy match is required

Given the following string;

"Hello", he said. "And now for something completely different!"(Monty Python).

I want to match "And now for something completely different!"(Monty Python), by making reference to quotation marks and brackets. In this example, "Hello" should not match, as there is no bracket following the quotation mark.

A non-greedy approach looks like one way to go: ".*?" matches "Hello" and "And now for something completely different!" separately, which is close, but if I append to this the brackets, ".*?"(.*), I end up matching the entire string.

That is

"Hello", he said. "And now for something completely different!"(Monty Python)

is returned.

How can I force my regex to behave the way I require? I am working in python so have the option of using lookahead/behind.

Upvotes: 2

Views: 189

Answers (2)

Anthony
Anthony

Reputation: 306

This works:

"[^"]*"(:?\(.*\))

It looks for a single set of quotes (i.e. no quotes in between) followed by the set of brackets

Upvotes: 2

Ajax1234
Ajax1234

Reputation: 71471

You can try this:

s = '"Hello", he said. "And now for something completely different!"(Monty Python).'

import re

new_data = re.findall('"(.*?)"', s)

final_data = [i for i in new_data if len(re.findall("\w+(?=!)", i)) > 0][0]

Output:

'And now for something completely different!'

Upvotes: 1

Related Questions