javascriptregextclregex-lookarounds

TCL I want to find a regex pattern to match only integers


I am using

(?<![\d.])[0-9]+(?![\d.])

This does the job for me but I can't use it as it says "couldn't compile regular expression pattern: quantifier operand invalid while executing"

I'm looking for any equivalents.


Solution

  • Tcl regex does not support lookbehinds, although it supports lookaheads.

    You can use something like

    set data {12  3.45 1.665 345}
    set RE {(?:^|[^0-9.])(\d+)(?!\.?\d)}
    set matches [regexp -all -inline -- $RE $data]
    foreach {- group1} $matches {
       puts "$group1"
    }
    

    Output:

    12
    345
    

    See the online demo. The (?:^|[^0-9.])(\d+)(?!\.?\d) regex matches