0

I have a file with following formatted file I need to parse

field_1 {
    field_2 {
        ....
    }
    field_i_want_to_replace {
        ....
    }
    ....
}
....

I need a pre-processor in python to parse those files and delete the content of some particular fields. In the above example, the processed file will look like:

field_1 {
    field_2 {
        ....
    }
    field_i_want_to_replace {}
    ....
}
....

So the preprocessor needs to locate the particular field "field_i_want_to_replace" and then delete the contents between the brackets. I'm trying to do the following but the regex can't parse the file correctly.

regex = r'(field_i_want_to_replace )\{.*?\}'
print re.sub(regex,'field_i_want_to_replace {}', file_in_string)

Is there sth wrong with the regex I'm using?

user2744486
  • 161
  • 1
  • 3
  • 10

1 Answers1

2

Your . character is not matching any newlines, so it will not continue after the left curly bracket.

To change this behavior, just add the re.DOTALL flag (or re.S) as a keyword arg to your re.sub:

>>> regex = r'(field_i_want_to_replace )\{.*?\}'
>>> print re.sub(regex,'field_i_want_to_replace {}', file_in_string, flags=re.DOTALL)
field_1 {
    field_2 {
        ....
    }
    field_i_want_to_replace {}
    ....
}
brianpck
  • 8,084
  • 1
  • 22
  • 33
  • Thanks. Forgot to mention that there could be nested curly in "field_i_want_to_replace". for example, field_i_want_to_replace { field_n{} } – user2744486 Oct 07 '16 at 21:06
  • 1
    In that case I don't think regex is the best tool at your disposal...see [this question](http://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns), unless you have more information. For instance, do you know what the next field is called? – brianpck Oct 07 '16 at 21:10
  • Thanks. Unfortunately I dont have any information on the nested fields. – user2744486 Oct 07 '16 at 21:15
  • Python's `re` doesn't support recursive calls but if you are able to import `regex` module into your project then there is a solution. @user2744486 – revo Oct 07 '16 at 21:28
  • @user2744486 Although you can use a different regex flavor, as I mentioned that's really not the right tool for this. – brianpck Oct 08 '16 at 00:49