Forum OpenACS Q&A: Tcl Advanced Regular Expresions (ARE).


I have the following doubts:

1) How works the the command "regexp -lineanchor" and "regexp -linestop"?

2) How do you parses with regexp an input with several lines (for example counting the amount of lines, or counting the amount of the word "a123b" inside of the text)?

Thanks in advance for the help!



Posted by Michael A. Cleverly on
Answers to both questions can be found by reading the man page for the regexp command. Reading the re_syntax man page is also helpful.

1a) The -lineanchor flag changes the behavior of the anchors ^ and $ making them match the beginning and end of a line (as opposed to the beginning and end of the entire string being matched).

1b) The -linestop flag changes the behavior of [^] (negative bracket expressions) and . (which matches anything) so that they will not match a newline on their own.

2) If you do [regexp -all $RE $input] the return value will be the number of matches.

Posted by Jorge Couchet on
Hi Michael,

First thanks for your fast response!

Regards (1):

The problem is that I don`t understand at all the utility of those flags just by explanation of the man page (for example: how are processed the different lines of the input?) . If for example you have the following string:

set str "tcl bla 12 aab\n tk bra 34 rt\n tck bka 12\n"

Could you give me an example of the use of -lineanchor and -linestop when the regexp's input is str?.

Regards (2):

I have put a bad example (counting the amount of matches), the question is: if you need to parse an input with several lines and extract for each line an string that matches a given pattern (that is, a loop that processes line by line, and of each line it extracts certain pattern and stores it). How obtain you that with regexp?.

Thanks again!