-2

please could you help with the regex to get everything unto the ";"..

window.egfunc = {
  name: "test name",
  type: "test type",
  storeId: "12345"
};

I have the following which works when all the data would be on one line, but as soon as there are returns, it won't work...

window.egfunc\s+=\s+(.*);
ChrisM
  • 309
  • 4
  • 19

2 Answers2

1

Replace the dot with [\s\S]:

window.egfunc\s+=\s+([\s\S]*?);
Toto
  • 83,193
  • 59
  • 77
  • 109
0

The dot . matches everything other than linefeed characters \n, \r. As soon as it encounters any LF character, it stops. We will have to explicitly say to regex to use . for multi-line using the flags re.DOTALL.

Similar modifiers are there in other languages too, like perl has s that needs to be added in last.

import re

txt = """window.egfunc = {
  name: "test name",
  type: "test type",
  storeId: "12345"
};"""

x = re.findall("window.egfunc\s+=\s+(.*);", txt, re.DOTALL)
print(x)

There is one more re.MULTILINE or re.M, but it didn't work alone. If anyone know the reason, can you please comment out?

Kamal Nayan
  • 1,682
  • 18
  • 31