Daniel James Smith
Daniel James Smith

Reputation: 65

Exclude multiple patterns with ^

I'm trying to search for strings of 250+ characters beginning with an opening apostrophe but not including a full stop and closing apostrophe; comma and closing apostrophe; exclamation point and closing apostrophe; or question mark and closing apostrophe. I'm trying to find long quotes but exclude short quotes (of under 250 characters). The problem is that a closing quotemark looks the same as a possessive apostrophe. (Maybe Americans are on to something with their double quotes?)

This is the code that works without the excluded punctuation marks .,!?

‘[^’]{230,}

This is my amended code (which doesn't work):

‘[^.’|,’|?’|!’]{230,}

This is so that I include phrases such as 'Charles's horse' but exclude quotes of under 250 characters

Upvotes: 0

Views: 636

Answers (2)

Daniel James Smith
Daniel James Smith

Reputation: 65

Thanks. I badly explained what I wanted, but this does what I asked. What I actually needed was a way of finding long quotes which didn't preclude long quotes containing an apostrophe s. This seems to work in the Regex demo, but doesn't in InDesign.

‘.{250,}(?=[.’])(?=[,’])(?=[?’])(?=[!’])

https://regex101.com/r/Um2Ylb/1

There are two long quotes (one with a possessive apostrophe s) and one short quote and it finds both long quotes. But in InDesign it just finds anything preceded by an apostrophe (it ignores the full point, question mark, comma, and exclamation mark). I think this is because it's only looking at a single character in the positive lookbehind. Is there a way of getting it to look for both characters?

Upvotes: 0

The fourth bird
The fourth bird

Reputation: 163632

You can match match and then repeat at least 230 times (Or 250+ times) matching any of the listed characters [.,?!] not directly followed by

‘(?:(?![.,?!]’)[^’]){230,}

The pattern matches

  • Match the opening
  • (?: Non capture group
    • (?![.,?!]’) Negative lookahead, assert not one of . , ? or !
    • [^’] Match any char except the closing `’
  • ){230,} Repeat 230+ times

Regex demo

If there has to be a closing quote at the end, you can assert that using a positive lookahead (?=’)

‘(?:(?![.,?!]’)[^’]){230,}(?=’)

Regex demo

Upvotes: 3

Related Questions