1

I have a string represents calcul and I want use regex for replace all data start with $.

Exemple :

value : $Var1+10*$Var2/$Var3-$Var4

I want 4 groups :

$Var1 $Var2 $Var3 $Var4

I tried this regex :

 \$.+?)[+-\/*]

It's ok for 3 first group ($Var1 $Var2 $Var3) but no for $Var4. It's because $Var4 is just before end of string and I don't find solution to add "end of string" in my list.

I tried :

\$.+?)[+-\/*$] 

\$.+?)[+-\/*\$]

but not work.

So I like to have any solution to add "end of string" in my list [].

Thanks,

Charly

er-sho
  • 9,581
  • 2
  • 13
  • 26
  • It is a well-known problem, you can't use zero-width assertions inside character classes, they lose their special meaning there. What you can do is an alternation group, or revamp the whole pattern altogether (preferable). `\$.+?)[+-\/*]` could not have worked since 1) there is an unpaired parenthesis, and 2) `-` creates a range, you must put `-` at the start/end of the class or escape it. – Wiktor Stribiżew Apr 09 '19 at 07:36

1 Answers1

2

If you want a regex which gives you the variable names then this would be it:

\$Var\d+

You don't have to add anything to start searching at the beginning or end of a string. Regex doesn't require this. For your test string "$Var1+10*$Var2/$Var3-$Var4" this will simply return all variable names. See here.

If your variable names don't necessarily contain Var but always start with a dollar sign then you can use the regex below to match them.

\$\w+
Vqf5mG96cSTT
  • 2,561
  • 3
  • 22
  • 41