1

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!

2
  • 1
    Are you looking to match OR split? Commented Nov 28, 2013 at 10:49
  • Also what language or tool are you using ? Commented Nov 28, 2013 at 10:51

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

10 Comments

+1 for taking a shot on something which wasn't a very clear problem.
@anubhava LOL. I was a little bored so I decided why not try to solve the issue, but Thank You of course.
Excellent, that solved my problem! +1 for extra explanation :)
@Sniffer I'm just trying to figure it out how it not include single quotes in the match when you are using non-capturing group?
@Sniffer No doubt you would it's not anything complicated, but as said he asked for not including them and still accepted your answer, thought it might be some regex language as you have mentioned difference or something. Anyway cheers ; )
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.