GOONhoon
GOONhoon

Reputation: 55

Python/Regex - match all sentences matching another regex expression

I use the following regex to match any square brackets containing digits inside them and then ten words preceding and following those brackets (?P<capture>(?:\w+\W+){,10}\[\d+\]\W+(?:\w+\W+){,10})

This would then return appellant had pleaded guilty: see R v McReady and Hurd [1978] 1 WLR 1376. In those circumstances, the legal advice would

Now I decided that looking for whole sentences makes more sense, so I want it to find all sentences, however long, including \[\d+\]\W+ in them.

Any help as how I would do that? I tried different methods but must have made mistakes along the way

Upvotes: 0

Views: 52

Answers (1)

Nazim
Nazim

Reputation: 639

My understanding of what you are looking for is, to find all the sentences that contain [number] in it. Try the below pattern with global and multiline match options on your text:

\s+[^.!?]*\[\d+\].*?[.!?]

For the example text below you should get two matches which are in bold color:

Appellant had pleaded guilty: see R v McReady and Hurd [1978] 1 WLR 1376. In those circumstances, the legal advice would. Is there another case [123]?

Upvotes: 1

Related Questions