0

I am working on a python app to create an interface for a script(ish) visual novel framework. For now, I was able to read the file, create the labels or functions but when I read them, it just reads one line. Sort of like this:

>>> from freddie import Freddie
>>> check = Freddie()
>>> check.read_script('script')
>>> check.read_label('start')
'label start:\n' # Only reads the first line and the whitespace

And the entire label is:

label start:

    test "Lorem ipsum"

    test "Sir amet"

return

My function is:

def read_label(self, label):
    search_label = re.search("label %s(.*)\s" % label, self.scripts.read(), re.MULTILINE)
    return search_label.group()

Is there a way to make the regular expression read the entire label and the whitespace?

Thanks for your time.

Pedro Romano
  • 10,973
  • 4
  • 46
  • 50
Leandro Poblet
  • 369
  • 1
  • 2
  • 11
  • I think http://stackoverflow.com/questions/587345/python-regular-expression-matching-a-multiline-block-of-text answers pretty much of your doubts. – resilva87 Nov 03 '12 at 03:43
  • Be sure to use raw strings in your regexes or character classes (like \s) won't work. – engineerC Nov 03 '12 at 03:44
  • @kaotik your link did part of the job, but it reads the first line only and I want the expression to read until the return statement. Maybe there is a way to modify it a bit to make the whole thing work? – Leandro Poblet Nov 03 '12 at 13:49

1 Answers1

1

The . in the regex normally matches any character except "\n", therefore the regex matches only the first line (the \s matches the "\n" at the end of that line).

If you want the . to match any character including "\n", use the DOTALL flag.

MRAB
  • 20,356
  • 6
  • 40
  • 33