Reputation: 149
I have a list of strings like this one:
A/B/C/P/E
I want to use a regex to capture only up to
A/B/C/P
and ignore the trailing /E
I tried using:
set mystring {A/B/C/P/E}
regex -nocase -- {(.*)\/\S+} $myString match
puts $match
but
puts $match
prints
A/B/C/P/E
What am I doing wrong?
Upvotes: 0
Views: 555
Reputation: 246807
an alternative: split the string, take all but the last element, re-join the list:
% set mystring {A/B/C/P/E}
A/B/C/P/E
% set new [join [lrange [split $mystring /] 0 end-1] /]
A/B/C/P
Upvotes: 0
Reputation: 71538
You are doing a couple of things wrong actually.
You have a string named $mystring
, but you are using $myString
in your function.
The syntax for regexp
is:
regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?
So you if you want the submatch, you need to use another variable.
Now, to make everything cleaner, you can use:
set myString {A/B/C/P/E}
regexp -- {(.*)/\S+} $myString -> match
puts $match
# => A/B/C/P
You don't need to escape the forward slash, and if you don't have character that have casing in your regex, you don't need the -nocase
flag.
The whole match is stored in the variable named ->
and the first submatch in match
.
Upvotes: 2