Reputation: 195
Hi I want to match only a part of a variable and store it. For example for the element b12asc34sx110 I want to match b12asc34s and store it in a variable and also store x110 in another variable. The value of the element keep changing, like it can be b12hdh56sx120 or b12hdgwa78x20. The code which I am trying is
set element b12asc34sx110
regexp [\(.*)\ {[^x(0-9]}] $element matched1
regexp [x(0-9)] $element matched2
puts $matched1
puts $matched2
The value of element will come from the user hence it may change everytim. I am new to regexp hence finding it difficult. I am not getting the output for it. Please help me with this.
Upvotes: 1
Views: 96
Reputation: 1927
Use a regexp like regexp {([^x]*)x([0-9]*)} "b12asc23sx110" matched1 sub1 sub2
.
Upvotes: 0
Reputation: 1798
Assuming "x" can't occur in the second part:
set element "b12asc34sx110"
if {[set i [string last "x" $element]] >= 0} {
set head [string range $element 0 $i-1]
set tail [string range $element $i end]
}
If "x" can occur in the second part but not in the first, change string last
to string first
.
Upvotes: 1
Reputation: 89547
You can do this:
#!/usr/bin/tclsh
set element "b12asc34sx110"
set pattern "(.*)(x.*$)"
if {[regexp $pattern $element whmatch sub1 sub2]} {
puts $sub1
puts $sub2
}
Notice:
if you have a several x in your string, the second capture group will begin at the last x, since the first quantifier is evaluated first and is greedy (default behavior), thus the first capturing group contains the largest possible substring.
If your are looking for the smallest first substring, use this pattern instead (with a lazy quantifier):
set pattern "(.*?)(x.*$)"
Upvotes: 1
Reputation: 2915
If the "x" is a constant then all you have to do is split at the x and put the first value in one and the second in another - regardless of what is in each side that changes but only if the "x" is a constant and will always appear. To get alpha and numeric since we don't have enough data to know if it will always be numeric on the right a lazy rule like (*)x(*)
should grab everything on the left of the x (assuming this is always present) and everything on the right.
Upvotes: 0