Huge
Huge

Reputation: 701

regEx matching a curly brace don't matched any way I try

solving a trivial task of finding the start of a body of a .php function, I'm not able to get a regEx match however I tried. Here's what I supposed to do the job:

import re
print re.search(r"addToHead(){", "addToHead(){\n\tcode...").group()
# addToHead is the function I'm looking for.
  # --> AttributeError: 'NoneType' object has no attribute 'group'
print re.search(r"addToHead()\{", "addToHead(){\n\tcode...").group()
  # Nor backslashing or double backslash works.
print re.search(r"addToHead()[\{]", "addToHead(){\n\tcode...").group()
print re.search(r"addToHead()[\x7b]", "addToHead(){\n\tcode...").group()
# Noting works...am I missing something??

Also I tried with re.DOTALL with the same unpleasant result. Do I sit on my nerve? Or a bug..?

Upvotes: 0

Views: 321

Answers (2)

Huge
Huge

Reputation: 701

Oh, now, just a minute after I posted the question I found it, it's not with the curly brace, but the standard brackets...well, I should likely delete my question, but [Meta-question] would I be able to access it as a record of my past blindness?

Upvotes: 0

thefourtheye
thefourtheye

Reputation: 239523

Brackets () are used to logically group the matched string in regular expression. Basically, they have special meaning in regular expressions. So you have to escape the brackets () like \(\).

print re.search(r"addToHead\(\){", "addToHead(){\n\tcode...").group()

Output

addToHead(){

Upvotes: 1

Related Questions