Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to regex match string escaped with sql style?

Tags:

regex

sql

examples:

"""Romeo and Juliet"""
'another string that is quoted with '' single quotes'

the problem is that the string can have characters used for db escaping even in it's beginning and end, so regex should look if given char is used in sequence of odd length that is 1,3,5... at the end of matched string

like image 745
rsk82 Avatar asked Jul 16 '11 16:07

rsk82


People also ask

How do you escape a string in RegEx?

The backslash in a regular expression precedes a literal character. You also escape certain letters that represent common character classes, such as \w for a word character or \s for a space.

Can you use RegEx with SQL?

You can use RegEx in many languages like PHP, Python, and also SQL. RegEx lets you match patterns by character class (like all letters, or just vowels, or all digits), between alternatives, and other really flexible options.

What does escape do in RegEx?

Escape converts a string so that the regular expression engine will interpret any metacharacters that it may contain as character literals.


1 Answers

Try this regex: '(?:[^']|'')*' for single quotes. The same for double quotes, i.e. full regex:

'(?:[^']|'')*'|"(?:[^"]|"")*"

In string hello 'my ''beautiful''' 'world'! """Romeo and Juliet""" it will find:

  1. 'my ''beautiful'''
  2. 'world'
  3. """Romeo and Juliet"""
like image 102
Kirill Polishchuk Avatar answered Sep 19 '22 20:09

Kirill Polishchuk