1

I'm trying to get the string between the symbols of a Regular Expression, but it seems to found nothing in the TextBox

var ph = txtCodigo.Text;
     foreach(Match m in 
         Regex.Matches(ph,@"${(.*?)}$",RegexOptions.IgnoreCase))
     {
         lstParams.Items.Add(m.Groups[1].ToString());
     }

I expect the string betwwen this symbols ${ }$, but the actual output is nothing

oguz ismail
  • 1
  • 16
  • 47
  • 69
  • Possible duplicate of [Regular Expression to find a string included between two characters while EXCLUDING the delimiters](https://stackoverflow.com/questions/1454913/regular-expression-to-find-a-string-included-between-two-characters-while-exclud) – Heretic Monkey Apr 29 '19 at 19:20
  • `$` is a special symbol in regex; it signifies the end of the string. Escape it to search for it. – Heretic Monkey Apr 29 '19 at 19:20

1 Answers1

0

$ is a special symbol in regexes, so much like special symbols in other places is must be escaped \$. To get something inside a tag but while excluding the tags, we use something in regex called a lookaround expression.

Regex rx = "(?<=\$\{).+?(?=\}\$)"

Explanation

(?<= find and exclude starting symbols, in this case ${. Technical name is positive lookbehind

.+? find any number of characters (special, whitespace, or otherwise)

(?= find and exclude closing symbols, in this case }$. Technical name is positive lookahead

Example on Regexr Test your regexes out on Regexr, it will save you a lot of headaches

TheBatman
  • 770
  • 4
  • 10
  • This one worked for me very well, thanks a lot, but now I have another problem, I have to now identify the name of the image (logo,jpg) in another regular expression, I have my image tags like this and the regular expression that I got in regex101.com is \ but it seems to return nothing again, help please =( – Enrique Portillo Apr 30 '19 at 14:05
  • Try this `(?<=$)` – TheBatman Apr 30 '19 at 14:46