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

    • (?:^|[^0-9.]) - start of string or a char other than a digit and a dot
    • (\d+) - Group 1: one or more digits
    • (?!\.?\d) - a negative lookahead that fails the match if there is an optional . and then a digit immediately to the right of the current location.