Issue
Input string could be :
- "his 'pet''s name is tom' and she is 2 years old"
- " '''' "
- " '' "
- "function('name', test, 'age')"
I want to get the single quote string from these inputs which may even contain ''
inside the single quote string.
I tried negative lookahead (?!')
to ignore the ''
while matching.
'.*?'(?!')
I expect output of
- 'pet''s name is tom'
- ''''
- 'name' and 'age'
Solution
I think you may achieve that with
r"'[^']*(?:''[^']*)*'"
See the regex demo
Explanation
'
- a single quotation mark[^']*
- 0+ chars other than single quotation mark(?:''[^']*)*
- zero or more repetitions of''
- two single quotation marks[^']*
- 0+ chars other than single quotation mark
'
- a single quotation mark
Answered By - Wiktor Stribiżew
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.