-1

I have this expression

if lbVar and ldcVar < 0.00 

And i need to match the right part of "and" that may contain parenthesis.

The expression to match without the parenthesis is already done:

 if(match(linha, /(\t| )*(and)(\t| )+(\<ldc\w*)[ \t]*(<)[ \t]*(ldc\w*|[0-9]|[0-9]+(\.[0-9]*))/) > 0){ 
        print "match: "substr($0, RSTART, RLENGTH);
    }

The problem is the expression may appear as:

if lbVar and ( ldcVar < 0.00 )

How can i correct my regex so that match both situation, that is, with or without parenthesis?

Thanks

2 Answers2

0

There are few typos in the sequence. Trying using the modified RE below

if(match(line, /(\t| )*(and)(\t| )(\(+)(\t| )*(\<ldc\w*)[ \t]*(<)[ \t]*(ldc\w*|[0-9]+|[0-9]+(\.[0-9]*))[ \t]*(\))/) ) {
    print "SUB: ="substr($0, RSTART, RLENGTH) "=" 
}

Also, consider changing the (\t| )* to [\t ]* (for efficiency/simplification).

dash-o
  • 13,723
  • 1
  • 10
  • 37
0

If this isn't all you need:

awk 'match($0,/.*[[:space:]]+and[[:space:]]+/){print substr($0,RLENGTH+1)}' file
( ldcVar < 0.00 )

then update your question to clarify your requirements and provide a more representative sample input/output that we can test a potential solution against for a simple pass/fail result.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • This isn't what i need. The regex must match the right part of the and , and match if the right part is between parenthesis, like the example: if lbVar and ( ldcVar < 0.00 ) – user2753544 Jan 21 '20 at 19:46
  • then update your question to better describe your requirements and provide a better example – Ed Morton Jan 22 '20 at 14:34
  • OK, there still isn't a good enough description or enough matching and non-matching in-context use cases present in your question for me personally to be able to help you more (1 sunny-day input example doesn't tell us much and I still don't know if the parentheses are required or not) but hopefully someone else will be able to. Good luck! – Ed Morton Jan 23 '20 at 15:29