如何在TCL中使用正则表达式找出字符串中的一个或多个字符
我需要一个简单的解决方案来确定某些字符是否在 Tcl 中的字符串中。 我的想法是用正则表达式来做到这一点。
我的字符串看起来像:“word_word-word_word_word-word
”或“word.word.word.word-word
”。 我的问题是,有时我得到包含 .
_
和 -
的字符串,然后我需要调用另一个过程来处理它。
现在又是一个问题,如何判断字符串中包含“_-_-
”或“...-
”以及 之间的任何单词_
.
-
I need a simple solution to figure out whether some characters are in a string in Tcl.
My idea is to do this with a regex.
My string looks like: "word_word-word_word_word-word
" or "word.word.word.word-word
".
My problem is, sometimes I get strings that contain .
_
and -
then i need to call another procedure to handle it.
Now the question again, how to figure it out that the string is contain "_-_-
" or "...-
" with any words between the _
.
-
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只是想查看字符串是否按顺序包含
_
、-
、_
、-
之间有任意随机垃圾,我们可以通过两种方式做到这一点(您可以替换其他分隔符,但是.
需要在正则表达式中进行特殊处理;[.]
或>\.
就可以了):好的,从技术上讲,最后一个(是全局匹配)匹配的内容略有不同,但效果相似。
但是我不确定以上内容是您真正想要的。你猜你真的是在寻找这个
单词
吗?也可能是分隔符。为了获取单词列表,我们使用了一种稍微不同的技术(不能使用
\w
因为它也匹配下划线,因为这在标识符中很常见):如果您也在分隔符之后,最简单的方法是使用
textutil::split::splitx
来自Tcllib:在最后一种情况下,输入字符串为
word_word-word_word_word-word
时,它会给出以下输出:If you were just looking to see if the string contains a
_
,-
,_
,-
in that order with arbitrary random junk between, we could do that two ways (you can substitute other separators, but a.
needs special treatment in a regexp; either[.]
or\.
will do):OK, technically the last one (which is glob matching) matches something slightly different, but the effect is similar.
However I'm not sure that the above is what you're really looking for. At a guess you're really after the
word
s? Possibly also the separators.To get a list of the words, we use a somewhat different technique (can't use
\w
as that matches underline as well because that's common in identifiers):If you're after the separators too, the easiest method is to use
textutil::split::splitx
from Tcllib:In the last case, with an input string of
word_word-word_word_word-word
it gives this output: