为什么我写的正则表达式不能正常工作?

时间:2019-12-25 12:29:57

标签: python regex

pattern = '(ns:m\.[^ ]+ )|(ns:g\.[^ ]+ )'
query = "PREFIX ns: <http://rdf.freebase.com/ns/>\nSELECT DISTINCT ?x\nWHERE {\nFILTER (?x != ns:m.0pz91)\nFILTER (!isLiteral(?x) OR lang(?x) = '' OR langMatches(lang(?x), 'en'))\nns:m.0pz91 ns:film.producer.film ?x .\n?x ns:film.film.genre ?c .\n?c ns:film.film_genre.films_in_this_genre ns:g.11b5lzm6b0 . \n}"
entities = re.findall(pattern, query)

我要做的是查找查询中的所有freebase实体,即示例中的'ns:g.11b5lzm6b0''ns:m.0pz91'。但是,我编写的代码返回[('ns:m.0pz91)\nFILTER ', ''), ('ns:m.0pz91 ', ''), ('', 'ns:g.11b5lzm6b0 ')]而不是['ns:m.0pz91 ', 'ns:g.11b5lzm6b0 ']
我通过使用两个单独的正则表达式(即ns:m\.[^ ]+ns:g\.[^ ]+)解决了该问题,但是,我仍然不明白为什么我不能直接使用(ns:m\.[^ ]+ )|(ns:g\.[^ ]+ )进行匹配ns:m\.[^ ]+ns:g\.[^ ]+

2 个答案:

答案 0 :(得分:5)

由于documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() { @Override public void onSuccess(DocumentSnapshot documentSnapshot) { if (documentSnapshot.exists()) { Map<String, Object> planMap = documentSnapshot.getData(); // how to parse the Object inside Map<String, Object> above to POJO Plan.class here } } } }); ,并且由于您匹配的内容不是 <!-- ##### Contact Area Start ##### --> <section class="contact-area bg-img section-padding-100-0" style="background-image: url('img/bg-img/plain.png');"> <div class="container"> <div class="row justify-content-between"> <!-- Contact Content --> <div class="col-12 col-lg-5"> <div class="contact-content mb-100"> <!-- Section Heading --> <div class="section-heading"> <p>Contact now</p> <h2><span>Get In Touch</span> With Us</h2> <img src="img/core-img/decor.png" alt="mango plant"> </div> <!-- Contact Form Area --> <div class="contact-form-area"> <form action="index.html" method="post"> <div class="row"> <div class="col-lg-6"> <input type="text" class="form-control" name="name" placeholder="Your Name"> </div> <div class="col-lg-6"> <input type="email" class="form-control" name="email" placeholder="Your Email"> </div> <div class="col-12"> <input type="text" class="form-control" name="subject" placeholder="Your Subject"> </div> <div class="col-12"> <textarea name="message" class="form-control" cols="30" rows="10" placeholder="Your Message"></textarea> </div> <div class="col-12"> <button type="submit" class="btn famie-btn">Send Message</button> </div> </div> </form> </div> </div> </div> </div> </div> </section> <!-- ##### Contact Area End ##### --> demo

,因此您的正则表达式无法正常工作

您可以参考演示网址右侧的说明。

相反,您可以尝试

\n

demo

更新

是否在原始正则表达式的输出中存在元组?

文档说:

  

如果模式包含多个组,这将是一个元组列表

您的正则表达式有两个捕获组。

答案 1 :(得分:2)

尝试:

\sns:m\.\w*|\sns:g\.\w*