Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx : Match a string enclosed in single quotes but don't match those inside double quotes

I wanted to write a regex to match the strings enclosed in single quotes but should not match a string with single quote that is enclosed in a double quote.

Example 1:

a = 'This is a single-quoted string';

the whole value of a should match because it is enclosed with single quotes.

EDIT: Exact match should be: 'This is a single-quoted string'

Example 2:

x = "This is a 'String' with single quote";

x should not return any match because the single quotes are found inside double quotes.

I have tried /'.*'/g but it also matches the single quoted string inside a double quoted string.

Thanks for the help!

EDIT:

To make it clearer

Given the below strings:

The "quick 'brown' fox" jumps
over 'the lazy dog' near
"the 'riverbank'".

The match should only be:

'the lazy dog'
like image 965
s4m0k Avatar asked Mar 25 '14 06:03

s4m0k


People also ask

Can string be enclosed in single quotes?

A double-quoted string can have single quotes without escaping them, conversely, a single-quoted string can have double quotes within it without having to escape them. Double quotes ( \" ) must escape a double quote and vice versa single quotes ( \' ) must escape a single quote.

How do you escape a double quote in regex?

Firstly, double quote character is nothing special in regex - it's just another character, so it doesn't need escaping from the perspective of regex. However, because Java uses double quotes to delimit String constants, if you want to create a string in Java with a double quote in it, you must escape them.

Do I need to escape single quote regex?

You do not need to backslash/escape the single quote.

How do you match expressions in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).


2 Answers

Assuming that won't have to deal with escaped quotes (which would be possible but make the regex complicated), and that all quotes are correctly balanced (nothing like It's... "Monty Python's Flying Circus"!), then you could look for single-quoted strings that are followed by an even number of double quotes:

/'[^'"]*'(?=(?:[^"]*"[^"]*")*[^"]*$)/g

See it live on regex101.com.

Explanation:

'        # Match a '
[^'"]*   # Match any number of characters except ' or "
'        # Match a '
(?=      # Assert that the following regex could match here:
 (?:     # Start of non-capturing group:
  [^"]*" # Any number of non-double quotes, then a quote.
  [^"]*" # The same thing again, ensuring an even number of quotes.
 )*      # Match this group any number of times, including zero.
 [^"]*   # Then match any number of characters except "
 $       # until the end of the string.
)        # (End of lookahead assertion)
like image 68
Tim Pietzcker Avatar answered Sep 18 '22 02:09

Tim Pietzcker


Try something like this:

^[^"]*?('[^"]+?')[^"]*$

Live Demo

like image 42
NeverHopeless Avatar answered Sep 20 '22 02:09

NeverHopeless