Zero
Zero

Reputation: 7

Remove line with specific word using tclsh

I have a paragraph like this:

"Nothing is worth more than the truth.
I like to say hello to him.
Give me peach or give me liberty.
Please say hello to strangers.
I'ts ok to say Hello to strangers."

I want to result:

"Nothing is worth more than the truth.
Give me peach or give me liberty."

if a line uses the word "hello" then remove that line and take only the line without that word.

I find some information in reference:

enter image description here

so I think ít as follow: regexp "^[^hello]" $line but it doesn't work

Upvotes: 0

Views: 62

Answers (1)

Chris Heithoff
Chris Heithoff

Reputation: 1863

There are several problems with your attempt of:

regexp "^[^hello]" $line
  1. A good practice with Tcl regular expressions is to put your regex inside curly braces instead of double quotes. Square brackets inside double quotes will be evaluated by Tcl as a command.

  2. ^ means the beginning of the line in regular expression.

  3. Characters inside square brackets in a regular expression are considered a "character-class". [^hello] does not mean the opposite of matching "hello". Instead, it matches a single character that is not h, e, l, or o.

  4. Do you care about case? If not, then add -nocase.

A Tcl expression, which you can use in an if statement to check that a line does not include "hello" (or "Hello") is simply this:

![regexp -nocase {hello} $line]

Upvotes: 1

Related Questions