Home > front end >  Regular expression in my code has problem
Regular expression in my code has problem

Time:10-09

I have a paragraph in design.txt: enter image description here I want to result: Hard Macros: Model Information Model width, height: 0.400 mm x 0.351 mm = 0.141 mm2 Aspect ratio (w/h): 1.14 Buckets: 239 x 210 = 50190 Total utilization: 72.4 % I tried with the code but it's not working: enter image description here

CodePudding user response:

Your code attempt from your included image is:

set fh [open "design.txt"];
while {[gets $fh line] >= 0} {
   if {[regexp {\[^\cell]} $line ]} {
      puts $line
   }
}

Please understand the difference between using curly braces and double quotes in Tcl.

It's good practice to use curly braces with regular expressions to avoid using so many backslashes.

These two Tcl regexes are acceptable ways to match an a, b, or c:

{[abc]}
"\[abc\]"

Not using a backslash inside double quotes, like "[abc]", is an error because Tcl will try to evaluate a command called abc.

You made the opposite mistake by adding too many backslashes inside curly braces. {\[abc]} only match a literal [abc].

You're also using regular expression character classes the wrong way. [^cell] does not mean the opposite of matching "cell". It means any single character not matching a c, e, or l. I don't understand why you used a backslash to escape the c either.

If you want to print every line that doesn't include the word cell:

set fh [open "design.txt"];
while {[gets $fh line] >= 0} {
   if {![regexp {cell} $line ]} {
      puts $line
   }
}
close $fh

CodePudding user response:

if I understood your goal correctly, you could achieve it just with grep:

grep -vi "cell" 

if you really want to fix your code, the path to regex mastery is long. the best tip I can give you is to create a test to verify your regex, while you develop it. You will gain speed troubleshooting and testing options while you are learning regex.

  • Related