Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex matching with regexp

Status
Not open for further replies.

redbanditos1999

Programmer
Oct 16, 2002
2
DE
Hi people !!

I need help for following problem:

how can I match "\%\w" in strings like

"hallo %hallo = 12U;"
"12 %12 = 0;"

but expect of strings like

printf("Today is the %d.%d.1998\n", pstruToday->sDay, pstruToday->sMonth);
printf("%d", xxx_ts_ExternCharArrayWrongType [2]);
printf ("%d >> 2 = %d\n", sSignedShift, sResult);

I need the "\%\w" to be relevant only outside of printf(...) sequences.


Many thanks for quick help !!!

redbanditos
 
proc NoPrintfMatches {fd} {
while {1} {
if {![eof $fd] && [gets $fd line] > -1} {
if {![regexp "printf\\(.*" $line]} {
if {[regexp "\%\\w" $line]} {
puts $line
}
}
} else {
break
}
}
close $fd
return
}

Good Luck
 
Thx for the answer Marsd !!

Im really sorry, but I didnt ever tell you what I really want to do ;-).
Im working on a syntax checker for C (Logiscope Tau Studio), where you can define your own rules for checking source files. The rules must be written in TCL mixed with special methods based on C-Language Data Model.
In my actual rule I need to match all binary operators, which dont have blanks after and before. So I have 2 regexp calls (after, before) which define regular expressions to find the operators. And now I only need the regular expression which allows me to find "%blahblah" or "%1234" in my source files text expect of using "%" in printf (like "%o" "%d" "%c" "%s" "%x").
I've tryed diverse expressions but I couldnt take advantage.

redbanditos
 
Okay I'm a little confused..
You are still saying that printf() calls are
to be discriminated against only and that scanf(),
sprintf, etc..are okay?
All of these get picked up now:
scanf(" %f", &myplanet.gravity);
scanf(" %f", &myplanet.size);
scanf(" %f%f", &j, &m);

This will end that:
[regexp "\[a-zA-Z\]+\\(.*\%.*\\)]"

This will catch your % instances:
[regexp \%.*]

As for catching your % inside parens:
The best way is to write another negation pattern,
trying to exclude preceding parens like:
"\[^\\(.*\%\]" or "\[^\\(\]..*\%" fail.

You could try this to exclude modulo inside parens.
![regexp "\\(.*%\\)"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top