开发者

TCL - obtain the list of strings separated by white space in another string using regular expressions

How to write a regexp in TCL that matches word an开发者_开发知识库d whitespaces. For example I have

aaaa    bbbb    cccc       

and I want to match "aaaaa ", "bbbb ", "cccc ". And also please tell me what is the regex symbol for whitespace and non-whitespace. I can't find it anywhere.

Thanks.


My thought would be to just search for groupings of word characters:

set text {aaaa bbbb cccc}
regexp -all -inline {\S+} $text
> aaaa bbbb cccc

You can find the writeup for the Tcl regular expression syntax on the re_syntax man page


I'm not quite sure exactly what you want, but here's an example:

set str "aaaa    bbbb    cccc       "
regexp {(\S+)\s+(\S+)\s+(\S+)} $str -> wordA wordB wordC
puts "The first is \"$wordA\", second \"$wordB\", and third \"$wordC\""

Which produces this output:

The first is "aaaa", second "bbbb", and third "cccc"

Within the RE, \S+ means a non-empty sequence of non-whitespace characters and \s+ means a non-empty sequence of whitespace. I could have used \w+ (“word” chars) and \W+ (“non-word” chars) respectively. The parentheses in the RE surround capture groups; Tcl does not require REs to match the whole input string.


Regex symbol for whitespace is " ". Like [a-z .] gives you a whitespace, as well as period and lowercase letters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜