Regex: Match double single quote inside string

我的未来我决定 提交于 2020-01-23 19:44:50

问题


I want a regex that will match for example 'panic can''t happen' as panic can''t happen. Double single quotes are just allowed if they're next to each other, 'panic can't' happen' sgould be divided into two strings, panic can and happen.

I got \'[^\']*[\'\']?[^\']\' so far but it won't work as expected.

Thanks!


回答1:


You can try the following:

'(?:[^']+|'')+'
  • ': Matches a literal '.
  • [^']+: Matches one or more characters which are not '.
  • '': Matches double ''.
  • (?:[^']+|'')+: Matches one or more occurrences of the preceding two patterns.
  • ' matches the closing '.

Regex101 Demo




回答2:


Well using this pattern will capture double single quotes or split when they are not next to each other:

PATTERN

'((?:''|[^']+)+)'

INPUT

'panic can't' happen'

OUTPUT

Match 1: 'panic can' 
Group 1: panic can

And:

Match 2: ' happen'
Group 1:  happen

2 case

INPUT

'panic can''t happen'

OUTPUT

Match 1: 'panic can''t happen' 
Group 1: panic can''t happen


来源:https://stackoverflow.com/questions/20263853/regex-match-double-single-quote-inside-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!