Haytham
Haytham

Reputation: 502

return only " from the selected expression

I have a regex rule [A-Z|a-z]"[A-Z|a-z].

When applying it to

Mother"s day passed!

the regex returns r"s, but I'm only interested in the " char that has to fall in the middle of alphabetic characters.

How can I do that with regex?

Upvotes: 2

Views: 31

Answers (2)

ElasticCode
ElasticCode

Reputation: 7875

You can use Positive Lookbehind as below

(?<=\b)"

\b assert position at a word boundary

" matches the character " literally

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627292

You may use lookarounds, a lookbehind coupled with a lookahead:

(?<=[A-Za-z])"(?=[A-Za-z])

Here, (?<=[A-Za-z]) positive lookbehind will require a letter on the left and the (?=[A-Za-z]) positive lookahead will require a letter to the right of the double quotation mark.

See the regex demo.

BTW, note that [A-Z|a-z] matches ASCII letters and a | char, since the | inside a character class loses its special meaning of an alternation operator.

Upvotes: 1

Related Questions