I need a regex that will match everything before a last dot in my string. For example, I have text like this:
if_blk4.if_blk1.if_blk1
I would like to get the if_blk4.if_blk1
.
Thanks!
I need a regex that will match everything before a last dot in my string. For example, I have text like this:
if_blk4.if_blk1.if_blk1
I would like to get the if_blk4.if_blk1
.
Thanks!
To match everything up to (but not including) the last dot, use a look ahead for a dot:
.*(?=\.)
The greedy quantifier *
makes the match include as of the input much as possible, while the look ahead (?=\.)
requires the next character in the input to be a dot.
How about
regexp {.*(?=\.[^.]*$)} $text match
i.e. matching any characters that lead up to a (not matched) sequence of dot and zero or more characters that aren't dots, followed by the end of the string.
(The regular expression {.*(?=\.)}
is equivalent as regular expression matching doesn't need to be anchored.)
or (faster)
file rootname $text
Documentation: file, regexp, Syntax of Tcl regular expressions