Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REGEXP Non Capturing Group is Capturing with TCL

Tags:

regex

tcl

I have data, see below, that I want to parse using REGEXP in my TCL script:

Mar 31 11:30:00 UTC+0100 2015
Mar 31 17:00:00 UTC+0100 2015
Mar 31 17:30:00 UTC+0100 2015
Apr 1 11:30:00 UTC+0100 2015
Apr 1 17:00:00 UTC+0100 2015
Apr 1 17:30:00 UTC+0100 2015
Apr 2 11:30:00 UTC+0100 2015

I want to just pull out the day of the month from the data above. This is the regular expression I have come up with so far and I don't know why it's not working.

(?:\w{3}\s)(\d{1,2})(?:\s)

I have tested it on the RegExr site with my sample data and it appears to work correctly there; it returns the day of the month in the group but when I run it in my TCL script it returns all of the match, i.e. the non capturing groups are being returned.

Is there something I'm missing here? I'm not great with regular expressions.

Thanks.

EDIT:

Below is the code sample from my script. I have a CSV file that I am reading from where sData6 column contains the date/time information above.

while {[gets $fInputFile line] >= 0} {
    set aAllOptions [split $line ,]
    lassign $aAllOptions sData1 sData2 sData3 sData4 sData5 sData6

    regexp -all {(?:\w{3}\s)(\d{1,2})(?:\s)} $sData6 regexData6

    puts "Printing regexp value $regexData6\n"

My puts statement is returning:

Mar 31 

I want it for just return:

31
like image 437
p_cos Avatar asked Dec 05 '25 11:12

p_cos


1 Answers

The regex you are using works fine, it is just a matter of syntax like I said in the comments:

regexp -all {(?:\w{3}\s)(\d{1,2})(?:\s)} $sData6 -> regexData6 
puts "Printing regexp value $regexData6\n"

The syntax is:

regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?

manpage

What you were using is matchVar, which contains the full match. subMatchVar contains the first captured group, and the one you needed.

I'm saving the matchVar in -> (practically any variable name could be used here and since I won't need it, I don't need something meaningful).


FWIW, you can use this shorter regex to achieve the same result:

regexp -all {\w{3}\s(\d{1,2})\s} $sData6 -> regexData6 
like image 84
Jerry Avatar answered Dec 08 '25 02:12

Jerry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!