python - Regex not matching pattern between quotes? -
i have simple regex parse source code files , each line extract content enclosed in double quotes, use in gettext.po file
here regex:
gettext_subject = re.compile(r"""[subject: |summary: ]\"(.*?)\"""").findall
here sample input file:
exports.onappointment = (appt, user, lang, isnew) -> if not user return promise.reject "appointment has no user." moment.locale(lang) start = moment(appt.when) cal = new ical() console.log appt.when cal.addevent start: start.todate() end: moment(start).add(2,"hours").todate() summary: "continental showroom visit" mail = to: user.emailid subject: if isnew "new appointment" else "appointment updated" alternatives: [ contenttype: "text/calendar", contents: new buffer(cal.tostring()), contentencoding: "7bit" ] template = name: "booking" lang: lang locals: name: "#{user.firstname} #{user.lastname}" datetime: moment(appt.when).format("dddd mmmm [at] hh:mm a") cancelurl: config.server.baseurl + "/appointment/cancel/#{appt._id}" emailclient.send2 mail, template
this code runs correct:
gettext_subject = re.compile(r"""subject: \"(.*?)\"""").findall
and testing command line returns, right answer
$ python python 2.7.6 (default, mar 22 2014, 22:59:56) [gcc 4.8.2] on linux2 type "help", "copyright", "credits" or "license" more information. >>> import re >>> gettext = re.compile(r"""[subject: |summary: ]\"(.*?)\"""").findall >>> pattern = """subject: \"blah blah blah\"\nsummary: \"summary text\"\nsubject: \"second subject line\"\nsummary: if isnew \"new appointment\" else \"appointment updated\"\n""" >>> print gettext(pattern) ['blah blah blah', 'summary text', 'second subject line', 'new appointment', 'appointment updated'] >>>
but when run through code not work, here code:
import os import sys import re operator import itemgetter walk_dir = ["app", "email", "views"] #t=(" ") gettext_messages = re.compile(r"""\"(.*)\"""", re.multiline).findall gettext_re = re.compile(r"""[=|#|{]t\(\"(.*?)\"""").findall gettext_subject = re.compile(r"""[subject: |summary: ]\"(.*?)\"""").findall gettext = [] x in walk_dir: curr_dir = "../node-blade-boiler-template/" + x root, dirs, files in os.walk(curr_dir, topdown=false): if ".git" in dirs: dirs.remove(".git") if "node-modules" in dirs: dirs.remove("node-modules") if "models" in dirs: dirs.remove("models") filename in files: file_path = os.path.join(root, filename) #print('\n- file %s (full path: %s)' % (filename, file_path)) open(file_path, 'rb') f: f_content = f.read() if 'messages.coffee' == filename: #pass msgids = gettext_messages(f_content) elif 'map.coffee' == filename: pass elif 'emailtrigger.coffee' == filename: #print f_content if 'subject: ' in f_content: print gettext_subject(f_content) msgids = gettext_subject(f_content) else: msgids = gettext_re(f_content) msgid in msgids: msgid = '"' + msgid + '"' #print msgid dic = { 'path' : file_path, 'msgid' : "%s" % msgid } gettext.append(dic)
any advice appreciated.
(?:subject:|summary:)[^"]*"(.*?)"
you can try this.see demo.[]
not think.its character class.[subject]
match subject
,tcejubs
charcters in order.
see demo.
Comments
Post a Comment