如何在 Tcl 中修复以下正则表达式? [英] how to fix the following regex in Tcl?

查看:32
本文介绍了如何在 Tcl 中修复以下正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 TCL 中有以下代码:

I have the following code in TCL:

"\\*05.|__|##|.T|__|__|"

尝试匹配以下输出:

*05 |__|##| T|__|__|

并且匹配.

但是如果输出是:

*05 |__|##|__|__|__|

它也匹配,是什么问题,如何解决?

it also matches, what is the problem, and how to fix it?

推荐答案

字符 | 是一个特殊字符,用于表示正则表达式中的或".你需要做的是逃避它.

The character | is a special character and is used to mean 'or' in regexp. What you need to do is escape it.

"\\*05.\\|__\\|##\\|.T\\|__\\|__\\|"

现在,为了避免所有这些双重转义,只需使用大括号即可!

Now, to avoid all those double escaping, just use braces!

regexp {\*05.\|__\|##\|.T\|__\|__\|} $string

<小时>

说明

如果你想要更深入的解释,你应该问.我不咬人!xD


Explanation

If you wanted a more in-depth explanation, you should have asked. I don't bite! xD

使用时:

regexp "\\*05.|__|##|.T|__|__|" "*05 |__|##| T|__|__|"

Tcl 正在调用命令 regexp 并且首先计算表达式(它在被带到实际命令 regexp 之前首先被处理,以及发送到 的内容正则表达式是:

Tcl is calling the command regexp and the expression is first evaluated (it is first processed before being taken to the actual command regexp and what is sent to regexp is:

\*05.|__|##|.T|__|__|

现在,由于 |regexp 中的意思是 ,命令将其计算为:

Now, since | means or in regexp, the command will evaluate it as:

  • 一个文字字符*,然后是05,然后是任意一个字符(换行符除外),或

  • One literal character *, then 05, then any one character (except newline), OR

两个_,或

两个#,或

任何字符后跟T, OR

两个_,或

两个_,或

什么都没有

然后将上面的每一个与您想要匹配的字符串进行比较,*05 |__|##|T|__|__|.

It then compares each of the above with the string you wanted to match, *05 |__|##| T|__|__|.

第一步:字符串中是否有*05.?是的,*05"在字符串中,因此匹配,所以它返回 1.

Step 1: is there *05. in the string? Yes, "*05 " is in the string and thus matches, so it returns 1.

当你将它与 *05 |__|##|__|__|__| 进行比较时,会发生同样的事情:

When you compare it to *05 |__|##|__|__|__|, the same thing happen:

第一步:字符串中是否有*05.?是的,*05"在字符串中,因此匹配,所以它返回 1.

Step 1: is there *05. in the string? Yes, "*05 " is in the string and thus matches, so it returns 1.

使用双重转义,任何计算后进入正则表达式的字符串是:

With double escaping, the string that goes to the regexp after any evalutation is:

\*05.\|__\|##\|.T\|__\|__\|

然后正则表达式将其读作:

The regexp then reads it as:

一个文字 * 字符,然后是 05,然后是任意字符,然后是文字 |,两个 _,一个文字|,两个#,一个文字|,任意字符,一个T,一个文字|,两个 _,一个文字 |,两个 _ 和一个文字 |.

One literal * character, then 05, then any character, then a literal |, two _, a literal |, two #, a literal |, any character, a T, a literal |, two _, a literal |, two _ and a literal |.

只有一个选项,因此当它与 *05 相比时 |__|##|T|__|__|,匹配.

There is only one option, thus when it compares to *05 |__|##| T|__|__|, it matches.

当它与*05 |__|##|__|__|__|比较时,当正则表达式检查T时,它不会找到一场比赛.

When it will compare it to *05 |__|##|__|__|__|, when the regex will check T, it won't find a match.

大括号防止表达式在被发送到正则表达式过程之前被评估.因此,该表达式将保持与您输入的相同.如果你把:

The braces prevent the expression to be evaluated before it is sent to the regexp procedure. Thus, the expression will remain the same as you have typed it out. If you put:

{\\*05.\\|__\\|##\\|.T\\|__\\|__\\|}

正则表达式将收到 \\*05.\\|__\\|##\\|.T\\|__\\|__\\| 并解释为 \ 0 次或更多次,然后是 05,然后是任何字符,\、OR 等....

The regexp will receive \\*05.\\|__\\|##\\|.T\\|__\\|__\\| and interpret is as a \ 0 or more times, then 05, then any character, a \, OR, etc....

这就是为什么不用大括号进行双重转义的原因:

This is why you don't double escape with braces:

{\*05.\|__\|##\|.T\|__\|__\|}

regexp 将收到的表达式是 \*05.\|__\|##\|.T\|__\|__\|,这是你在"\\*05.\\|__\\|##\\|.T\\|__\\|__\\|" 之前处理过.

And the expression that regexp will receive is \*05.\|__\|##\|.T\|__\|__\|, which is the one you had after the "\\*05.\\|__\\|##\\|.T\\|__\\|__\\|" was processed earlier.

这篇关于如何在 Tcl 中修复以下正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆