Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to get text BETWEEN two characters

Tags:

regex

PLEASE READ CAREFULLY: This is a bit unusual and you will be tempted to say things like "that isn't how to use a regex" or "dude, just use String.SubString()," Etc...

I have a need to write a regex (to use a pre-existing method) that will match text BETWEEN curly braces, BUT NOT the curly braces themselves.

For Example: "{MatchThisText}" AND "La la la {MatchThisText} la la la..."
Should Both Match: "MatchThisText"

Someone asked this exact question a year ago, and he got a bunch of solutions for regexes that WILL match the curly braces in addition to "MatchThisText," resulting in a match of "{MatchThisText}" which is not what he (or I) needed.

If someone can write a Regex that actually matches only the characters BETWEEN the curly braces, I'd really appreciate it. It should allow any ASCII values, and should stop the match at the FIRST closing bracket.

For Example: "{retailCategoryUrl}/{filters}"
Should Match: retailCategoryUrl and filters
But NOT Match: "retailCategoryUrl}/{filters" (Everything but the outer braces)

Hey, this is a really tricky one for me, so please forgive the question if this is trivial for some of you.

THANKS!

like image 670
bopapa_1979 Avatar asked Oct 26 '10 18:10

bopapa_1979


People also ask

How do you use wildcards in regex?

In regular expressions, the period ( . , also called "dot") is the wildcard pattern which matches any single character. Combined with the asterisk operator . * it will match any number of any characters.

What does \\ mean in regex?

\\. matches the literal character . . the first backslash is interpreted as an escape character by the Emacs string reader, which combined with the second backslash, inserts a literal backslash character into the string being read. the regular expression engine receives the string \.

How do I find a character in a string 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

Python:

(?<={)[^}]*(?=})

In context:

#!/usr/bin/env python

import re

def f(regexStr,target):
    mo = re.search(regexStr,target)
    if not mo:
        print "NO MATCH"
    else:
        print "MATCH:",mo.group()

f(r"(?<={)[^}]*(?=})","{MatchThisText}")
f(r"(?<={)[^}]*(?=})","La la la {MatchThisText} la la la...")

prints:

MATCH: MatchThisText
MATCH: MatchThisText
like image 95
Douglas Leeder Avatar answered Sep 23 '22 23:09

Douglas Leeder


If you're using a RegExp engine with lookahead and lookbehind support like Python, then you can use

/(?<={)[^}]*(?=})/

If it doesn't (like javascript), you can use /{([^}]*)}/ and get the substring match. Javascript example:

"{foo}".match(/{([^}]*)}/)[1] // => 'foo'

like image 34
Daniel Mendel Avatar answered Sep 24 '22 23:09

Daniel Mendel