TCL Expect Using Regular Expressions to Extract String - regex

I'm trying to extract strings from a file that match the following format:
AP[1st nibble].[2nd nibble].[3rd nibble]
For example: AP30f7.0df6.e51c
The code below captures all data sharing the same line as the above string. What can I do to stop capturing any undesired data found on the same line as the above string?
while { [gets $fchan inline] >= 0} {
switch -regexp -- $inline {
AP([a-f0-9]{4}\.[a-f0-9]{4}\.[a-f0-9]{4}) {
append default_name $inline\n
}
}
}
UPDATE:
Found a work around. Since each line matching the condition I've specified starts with the desired string, I'll use the string range command to extract only the first 16 characters.
while { [gets $fchan inline] >= 0} {
switch -regexp -- $inline {
AP([a-f0-9]{4}\.[a-f0-9]{4}\.[a-f0-9]{4}) {
set inline_mod [string range $inline 0 15]
append default_name $inline_mod\n
}
}
}

The switch command has some useful options when you want to do extraction at the same time as matching an RE. In particular, you should use the -matchvar option.
while { [gets $fchan inline] >= 0} {
switch -regexp -matchvar matched -- $inline {
AP([a-f0-9]{4}\.[a-f0-9]{4}\.[a-f0-9]{4}) {
# Extract the first and second elements
lassign $matched inline_mod triple
# With your sample of AP30f7.0df6.e51c
# $inline_mod is "AP30f7.0df6.e51c"
# $triple is "30f7.0df6.e51c"
append default_name $inline_mod\n
}
}
}
There are some further examples on that manual page.

Related

Regular expression is too complex error in tcl

I have not seen this error for a small list. Issue popped up when the list went >10k. Is there any limit on the number of regex patterns in tcl?
puts "#LEVELSHIFTER_TEMPLATES_LIMITSFILE:$perc_limit(levelshifter_templates)"
puts "#length of templates is :[llength $perc_limit(levelshifter_templates)]"
if { [regexp [join $perc_limit(levelshifter_templates) |] $temp] }
#LEVELSHIFTER_TEMPLATES_LIMITSFILE:HDPELT06_LVLDBUF_CAQDP_1 HDPELT06_LVLDBUF_CAQDPNRBY2_1 HDPELT06_LVLDBUF_CAQDP_1....
#length of templates is :13520
ERROR: couldn't compile regular expression pattern: regular expression is too complex
If $temp is a single word and you're really just doing a literal test, you should invert the check. One of the easiest ways might be:
if {$temp in $perc_limit(levelshifter_templates)} {
# ...
}
But if you're doing that a lot (well, more than a small number of times, 3 or 4 say) then building a dictionary for this might be best:
# A one-off cost
foreach key $perc_limit(levelshifter_templates) {
# Value is arbitrary
dict set perc_limit_keys $key 1
}
# This is now very cheap
if {[dict exists $perc_limit_keys $temp]} {
# ...
}
If you've got multiple words in $temp, split and check (using the second technique, which is now definitely worthwhile). This is where having a helper procedure can be a good plan.
proc anyWordIn {inputString keyDictionary} {
foreach word [split $inputString] {
if {[dict exists $keyDictionary $word]} {
return true
}
}
return false
}
if {[anyWordIn $temp $perc_limit_keys]} {
# ...
}
Assuming you want to see if the value in temp is an exact match for one of the elements of the list in perf_limit(levelshifter_templates), here's a few ways that are better than trying to use regular expressions:
Using lsearch`:
# Sort the list after populating it so we can do an efficient binary search
set perf_limit(levelshifter_templates) [lsort $perf_limit(levelshifter_templates)]
# ...
# See if the value in temp exists in the list
if {[lsearch -sorted $perf_limit(levelshifter_templates) $temp] >= 0} {
# ...
}
Storing the elements of the list in a dict (or array if you prefer) ahead of time for an O(1) lookup:
foreach item $perf_limit(levelshifter_templates) {
dict set lookup $item 1
}
# ...
if {[dict exists $lookup $temp]} {
# ...
}
I found a simple workaround for this problem by using a foreach statement to loop over all the regexes in the list instead of joining them and searching, which failed for a super-long list.
foreach pattern $perc_limit(levelshifter_templates) {
if { [regexp $pattern $temp]}
#puts "$fullpath: [is_std_cell_dev $dev]"
puts "##matches: $pattern return 0"
return 0
}
}

How to check is Jenkins pram contains a character

I am trying to check if my Jenkins parameter contains a hostname.
But when I use Regular Expressions to see if it contains the name it doesn't check.
I would guess I have an error in the way I am checking or how I have it wrapped in brackets.
Below is a sample of what I am working with
stage('Release 1') {
when {
expression { params.SECRET_NAME != "" && params.STAGING_ENV != ("*some.host.name*") }
}
steps {
echo "Release 1"
}
}
stage('Release 2') {
when {
expression {params.STAGING_ENV == ("*some.host.name*") && params.SECRET_NAME == ("*+*") }
}
steps {
echo "Release 2"
}
}
}
I want it to skip the stage in my Jenkins pipeline if it does not meet the conditions
Ok, you need multiple changes here, from inside out:
Replace the * with .*. Simply put, in regex * denotes the same (set) of characters any number of times (abc* matches abccccc), whereas .* denotes any character any number of times (abc.* matches abccccc, abcdefg, abcadkhsdalksd, etc.).
Remove the double quotes " surrounding the regex patterns; lest you want them to be interpreted as string literals.
Wrap the regex patterns within delimiters, usually / to define the string boundary.
The brackets () themselves are optional here.
To match regular expressions, replace the equal operator == with the match operator ==~ (strict), which returns a boolean.
There is no "NOT match" operator in Groovy. To invert the match, you need to invert the result of the entire expression.
If the + in *+*should be a literal, then you must escape it as *\+*.
Stitching these together, your pipeline should look like:
stage('Release 1') {
when {
expression {
params.SECRET_NAME != "" && !(params.STAGING_ENV ==~ /.*some.host.name.*/)
}
}
steps {
echo "Release 1"
}
}
stage('Release 2') {
when {
expression {
params.STAGING_ENV ==~ /.*some.host.name.*/ && params.SECRET_NAME ==~ /.*\+.*/
}
}
steps {
echo "Release 2"
}
}
Further reading:
http://docs.groovy-lang.org/latest/html/documentation/core-operators.html
http://web.mit.edu/hackl/www/lab/turkshop/slides/regex-cheatsheet.pdf

Regexp - find a value shown after string, (TCL)

I want to return a value to $output, from out_buffer, so i did :
set output ""
set out_buffer {Unevictable: 0 kB}
#regexp -line {Unevictable:.* (.*\d).*KB} $out_buffer dummy output
if {!($output == "0")} {
return 0
} else {
puts "Unevictable is OK (equal 0)"
}
It works fine, but if out_buffer is like:
set out_buffer {cat /proc/meminfo | grep Unevictable
Unevictable: 0 kB
root#ltqcpe:/ramdisk/tmp# }
the return is null. What can I do ? that in any combination the value after Unevictable: will be put into $output.
You probably want to use the -line option to regexp so that ^ and $ are line-aware. (Maybe the -nocase option too.) Then you can do this (which I've tested with both your sample input strings):
regexp -line -nocase {^Unevictable:\s*(\d+)\s*kB$} $out_buffer -> size
Also remember to check the result of regexp; it's the number of times the RE matched, which is 0 or 1 (conveniently boolean!) unless you also pass in the -all option.
There can be many ways to write regular expressions to match your string. Try something like
if {regexp {Unevictable:\s+(\d+)\s+kB} $out_buffer ignore size } {
puts "size = $size"
}

Creating a list in tcl with elements that in proper index positions

How Do I convert the below string/list to a list whose first element is 1-81 second element is 81-162 3rd element us 162-243 using tcl
{} {} {1 -81} { } {81 -162} { } {162 -243} { } {243 -324} { } {324 -405} { } {405 -486} { } {486 -567} { } {567 -648} { } {648 -729} { } {729 -810} { } {810 -891} { } {891 -972} { } {972 -1053} { } {1053 -1134} { }
Thanks
If you just want to filter out empty list elements, the obvious thing to do is:
# Assuming the original list is in $list
set result {}
foreach x $list {
if {[string trim $x] != ""} {
lappend result $x
}
}
# The result list should contain the cleaned up list.
Note that you don't need to do the [string trim] if you're sure all empty elements really are empty and don't contain whitespace (meaning {} instead of possibly { }). But your example contain both empty elements and whitespace so you need to do the string trim.
Alternatively you can use a regular expression to test:
foreach x $list {
# Test if $x contains non-whitespace characters:
if {[regexp {\S} $x]} {
lappend result $x
}
}
You can however do the above in a single line using lsearch:
# Find all elements that contain non whitespace characters:
set result [lsearch -inline -all -regexp $list {\S}]
It seems you want to accomplish two goals:
Remove all empty items from the original list
For each non-empty item, remove space
I would like to offer a different approach: using the struct::list, which has a filter command:
package require struct::list
set oldList {{} {} {1 -81} { } {81 -162} { } {162 -243} { } {243 -324} {}}
set newList [struct::list filterfor item $oldList {
[set item [string map {{ } {}} $item]] != ""
}]
In this solution, I use the struct::list filterfor command, which resembles the foreach command. The body of the filterfor is a boolean expression. In the body, I use string map to remove all spaces from each item, and only return true if the result is not empty. This solution might not be the most efficient, but a different approach to solve the problem.

How to match the variable in switch with contents of a list?

I have a doubt concerning the use of switch in tcl. Mainly, I was wondering if it was possible to make something like:
switch myvar {
list1 {
puts "myvar matches contents of list1"; }
list2 {
puts "myvar matches contents of list2"; }
default {
puts "myvar doesn't match any content of any list"; }
}
In here, list1 and list2 would be either a list or array of strings containing the names of different files.
Is this even possible without making a very detailed regexp search?
Thanks!
You can rewrite it as an if elseif else construct easily, as Brian Fenton already said (and simplify it with the 'in' operator too.
if {$myvar in $list1} {
puts "myvar matches content of list"
} elseif {$myvar in $list2} {
puts "myvar matches content of list2"
} elseif {
puts "myvar doesn't match any content of any list"
}
You could of course wrap up the code and write your own switch version that does what you want, after all, this is Tcl...
proc listswitch {item conditions} {
if {[llength $conditions] % 2} {
return -code error "Conditions must be pairs"
}
set code ""
foreach {cond block} $conditions {
if {$cond eq "default"} {
set code $block
break
} elseif {$item in $cond} {
set code $block
break
}
}
if {$code ne ""} {
uplevel 1 $code
}
}
listswitch 10 {
{10 20 30 50} {
puts "Match in list 1" }
{50 20 90 11} {
puts "Match in list 2"
}
default {
puts "No match"
}
}
You need to worry a little if you want to match filenames literally, or what kind of equality your interested in though. There are some subtle things there, like case insensitive filesystems, different directory separators, absolute vs. relative and even stuff like filesystem encodings which might change the outcome.
Nice question Jason. At first, I thought you wanted a way to compare the contents of two lists. But I think you want to check if the string is a member of the lists. I don't see any easy way to do that with switch, so what I would do is very simply to use lsearch.
if {[lsearch $list1 $myvar ] != -1} {
puts "myvar matches contents of list1"; }
} elseif {[lsearch $list2 $myvar ] != -1} {
puts "myvar matches contents of list2"; }
} else
puts "myvar doesn't match any content of any list"; }
}