UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

Reply

Variables in regexp expressions

Highlighted
Contributor
Posts: 23
Registered: ‎09-07-2016

Variables in regexp expressions

I was trying to construct a regexp expression from a variable. I turned out to be challenging to include parenthesis and a variable in the regexp expression.

 

I figured that:

 

filter -regexp -nocase $list_of_objects PROPERTY\ =~\ "(.*:)?${name}(:.*)?"

 

works, but 

 

filter -regexp -nocase $list_of_objects [list PROPERTY =~ "(.*:)?${name}(:.*)?"]

 

does not. Can anyone explain to me why exactly this behaves differently?

Explorer
Posts: 187
Registered: ‎09-07-2011

Re: Variables in regexp expressions

Do not know fundamentally, but from practise know that in the first example the quotes are just literal characters.   In the second one, the quotes denote a string.   Difference can be seen:

 

set name ABC 
puts PROPERTY\ =~\ "(.*:)?${name}(:.*)?"
puts [list PROPERTY =~ "(.*:)?${name}(:.*)?"]

The filter commands wants to see a string with quotes inside it.   Adding quotes might be ugly:

 

puts [join [list PROPERTY =~ "\"(.*:)?${name}(:.*)?\""] { } ]

 

Sometimes to avoid quote/escape hell, something like this works:

set prop {PROPERTY =~ "(.*:)?%NAME%(:.*)?"}
regsub %NAME% $prop $name prop
puts $prop

 

 But your first example is pretty compact and to the point..

 

 

Explorer
Posts: 187
Registered: ‎09-07-2011

Re: Variables in regexp expressions

Actually, "format" might be easier.  Works like printf.

 

puts [format {PROPERTY =~ "(.*:)?%s(:.*)?"} $name]