Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 需要关于TCL中正则表达式的帮助吗_Regex_Tcl - Fatal编程技术网

Regex 需要关于TCL中正则表达式的帮助吗

Regex 需要关于TCL中正则表达式的帮助吗,regex,tcl,Regex,Tcl,有人能帮我介绍一下TCL中下面正则表达式的“执行流程”吗 % regexp {^([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])$} 9 1 (success) % % % regexp {^([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])$} 64 1 (success) % regexp {^([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])$} 255 1 (success) % regexp {^([01]

有人能帮我介绍一下TCL中下面正则表达式的“执行流程”吗

% regexp {^([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])$} 9
1 (success)
%
%
% regexp {^([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])$} 64
1 (success)
% regexp {^([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])$} 255
1 (success)
% regexp {^([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])$} 256
0 (Fail)
% regexp {^([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])$} 1000
0 (Fail)

谁能解释一下这些是如何执行的?我正在努力理解。

一切都在详细说明中


regexp首先在这里用括号表示的主捕获组周围有锚点
^
$
([01]?[0-9][0-9]?| 2[0-4][0-9]| 25[0-5]),这意味着它正在检查整个字符串

其次,在捕获组中,我们有3个部分:

[01]?[0-9][0-9]?

2[0-4][0-9]

25[0-5]

它们用
|
(或)运算符分隔,这意味着如果字符串满足这三个部分中的任何一个,则匹配成功

现在,对于各个部分:

  • [01]?[0-9][0-9]?
    这意味着它匹配0或1次[01](0或1),然后是任意数字,如果有,则再次匹配任意数字。总之,它接受像
    000
    199
    这样的字符串,但不接受199以上的字符串

  • 2[0-4][0-9]
    这遵循与上述相同的逻辑,只是它验证数字从200到249的字符串

  • 25[0-5]
    最后,这个函数验证250到255之间的字符串

  • 由于没有更多内容,只有从
    000
    255
    的数字才能成功验证


    这就是为什么通过了9、64和255,而不是256或1000。

    它与以下数字匹配

    [01]?[0-9][0-9]? -> 0 - 9, 00 - 99, 000 - 199
    2[0-4][0-9]      -> 200 - 249
    25[0-5]          -> 250 - 255   
    

    这不是问题的答案,只是探索其他方法进行验证:

    proc from_0_to_255 {n} {
        expr {[string is integer -strict $n] && 0 <= $n && $n <= 255}
    }
    from_0_to_255 256          ; # => 0
    
    proc从\u 0\u到\u 255{n}{
    
    expr{[string is integer-strict$n]&&0一个更好的链接(它是Tcl的等效资源),尽管RE语言在问题中使用的子集上是相同的。
    proc from_0_to_255 {n} {
        expr {[string is integer -strict $n] && 0 <= $n && $n <= 255}
    }
    from_0_to_255 256          ; # => 0
    
    proc int_in_range {n {from 0} {to 255}} {
        expr {[string is integer -strict $n] && $from <= $n && $n <= $to}
    }
    int_in_range 256           ; # => 0
    int_in_range 256  0 1024   ; # => 1
    
    proc int_in_range {n args} {
        array set range [list -from 0 -to 255 {*}$args]
        expr {
            [string is integer -strict $n] &&
            $range(-from) <= $n && $n <= $range(-to)
        }
    }
    int_in_range 256           ; # => 0
    int_in_range 256 -to 1024  ; # => 1