Reputation: 91
I can use \p{Punct}
to match all punctuations(including underscore).
And I wanted to exclude all apostrophes strictly inside a word. For this I'm using (?<=[a-zA-Z])'(?=[a-zA-Z])
However I couldn't have them work together to match all punctuations except apostrophes strictly inside a word.
What should I use?
examples:
my brother's
this should not match.
my brothers'
this should match.
my 'brother'
these should match.
Upvotes: 4
Views: 2745
Reputation:
You can combine three conditions here.
Match all punctuation except apostrophe '
using [\p{Punct}&&[^']]
Match all apostrophe not followed by a letter.
Match all apostrophe not preceded by a letter.
Regex: [\p{Punct}&&[^']]|(?<![a-zA-Z])'|'(?![a-zA-Z])
Explanation:
[\\p{Punct}&&[^']]
excludes apostrophe from punctuation class.
(?<![a-zA-Z])'
matches apostrophe not preceded by a letter.
'(?![a-zA-Z])
matches the apostrophe not followed by a letter.
Upvotes: 5
Reputation: 564
You could group all punctuations, you are interested in, manually and exclude the apostrophe. Then combine that group with your rule for finding the right apostrophes (that are not within a word) by an OR.
Upvotes: 0