• Post Reply Bookmark Topic Watch Topic
  • New Topic

help on pattern.compile  RSS feed

 
catherine matthews
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


HI.... WHY I CANNOT GET THE ANY RESULT IF I AM DECLARING THE PATTERN LIKE THIS?("'if'|'for'|'switch'|'while'|'do'|'case'")

BUT I CAN RECOGNIZE ALL THE WORDS IF I COMPILE LIKE THIS
("if|for|switch|while|do|case")
BUT THEN IT ALSO RECOGNIZES THE WORD double as one of the predicate... why is it so?
 
pradeep selvaraj
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i am not sure about the regx part. But i have been working on a program that calculates cc my self and think that a cc count should also include exceptions thrown & catch blocks.
Good Luck
[ June 22, 2006: Message edited by: pradeep selvaraj ]
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI.... WHY I CANNOT GET THE ANY RESULT IF I AM DECLARING THE PATTERN LIKE THIS?("'if'|'for'|'switch'|'while'|'do'|'case'")


The quote (single or double), does not have special meaning in a regular expression. If you have it in your regex, it will try to match a quote -- just like any letter.

BUT I CAN RECOGNIZE ALL THE WORDS IF I COMPILE LIKE THIS
("if|for|switch|while|do|case")
BUT THEN IT ALSO RECOGNIZES THE WORD double as one of the predicate... why is it so?


The problem is the "do" part of the regular expression, it is matching the first two letters of "double". If you want to specify that the "do" is a whole word, then you also need to specify the word boundaries with the "\\b" pattern -- meaning the word "do" should be "\\bdo\\b".

Henry
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
catherine matthews
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ya... it helps
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!