TCL - get a list of lines separated by a space in another line using regular expressions

How to write a regular expression in TCL that matches a word and spaces. For example, I have

aaaa bbbb cccc 

and I want to combine "aaaaa", "bbbb", "cccc". And also, please tell me what is the regular expression symbol for spaces and spaces. I can’t find anything.

Thanks.

+4
source share
3 answers

My thought would be to search for word character groups:

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

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

+4
source

I'm not quite sure what you want, but here is 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\"" 

What produces this conclusion:

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

In RE, \S+ means a non-empty sequence of non-white characters, and \S+ means a non-empty sequence of spaces. I could use \w+ ("words" characters) and \w+ ("non-words" characters) respectively. Brackets in RE READ capture groups; Tcl does not require REs to match the entire input string.

+2
source

The regex for spaces is "". Like [az.], You get spaces, as well as period and lowercase letters.

-1
source

Source: https://habr.com/ru/post/1340632/


All Articles