简体   繁体   English

正则表达式不匹配引号之间的模式?

[英]Regex not matching pattern between quotes?

I have a simple regex to parse source code files and for each line extract the content enclosed in double quotes, to use in gettext.po file 我有一个简单的正则表达式来解析源代码文件,并为每行提取用双引号引起来的内容,以在gettext.po文件中使用

Here is my regex: 这是我的正则表达式:

gettext_subject = re.compile(r"""[subject: |summary: ]\"(.*?)\"""").findall

Here is a sample input file: 这是一个示例输入文件:

exports.onAppointment = (appt, user, lang, isNew) ->
  if not user then 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 then "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 Do 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 this from the command line also returns, the 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" for 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 then \"New appointment\" else \"Appointment updated\"\n"""
>>> print gettext(pattern)
['blah blah blah', 'summary text', 'second subject line', 'New appointment', 'Appointment updated']
>>> 

but when I run it through my code this does not work, here is the code: 但是,当我通过代码运行它时,它不起作用,下面是代码:

import os
import sys
import re
from 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 = []
for x in walk_dir:
    curr_dir = "../node-blade-boiler-template/" + x
    for 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")

        for filename in files:
            file_path = os.path.join(root, filename)
            #print('\n- file %s (full path: %s)' % (filename, file_path))
            with open(file_path, 'rb') as 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)
                for msgid in msgids:
                    msgid = '"' + msgid + '"'
                    #print msgid
                    dic = {
                    'path' : file_path,
                    'msgid' : "%s" % msgid
                    }
                    gettext.append(dic)

any advice much appreciated. 任何建议,不胜感激。

(?:Subject:|Summary:)[^"]*"(.*?)"

You can try this.See demo. 您可以尝试一下。请参阅演示。 [] is not what you think.Its character class. []不是您所想的。它是字符类。 [subject] will match subject , tcejubs any charcters in any order. [subject]将匹配subject ,以任何顺序排列任何tcejubs

See demo. 参见演示。

https://regex101.com/r/mT0iE7/33#python https://regex101.com/r/mT0iE7/33#python

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM