Now I have a list of pattern:
patterns = ['php', 'java', 'c++']
and I want to match it in another string, say, r'c++ primer'. I want to use python re module to do it, but the problem is, if I use:
for pattern in patterns:
re.findall(pattern, r'php php java java c++ c++')
I will get an error because '+' has special meanning in regular expression.
So how can I fix something like c++
or c*
in this situation?
Notice that I have a lot of patterns to match so I don't want to convert everything like c++
to c\+\+
manually.
Thanks for your attention.
\+\+
was not what the asker is looking for. But you SHOULD do a Ctrl+H search with "c++" and replace with "c\+\+". – leewangzhong Dec 8 '13 at 7:51for p in patterns: print "php php java java c++ c++".count(p)
will show how many times the string occurs, orfor p in patterns: print p in "php php java ..."
will show if the string contains the pattern at all – dbr Dec 8 '13 at 8:36