【问题标题】:Regular expression to skip all <html> tags and search in remaining text正则表达式跳过所有 <html> 标签并在剩余文本中搜索
【发布时间】:2014-02-21 05:07:24
【问题描述】:

我正在使用 MongoDB,其中有一个名为 Companies 的集合,它的文档如下-

    {
        "_id":1,
        "name": "Innovative Software pvt. limited 1",
        "description": "This is a software company"
    }

    {
        "_id":2,
        "name": "Innovative Software pvt. limited 2",
        "description": "This is a software,company with <img src='' class='' alt='company logo' /> symbol"
    }

    {
        "_id":3,
        "name": "Innovative Software pvt. limited 3",
        "description": "This is a software, company with <img src='' class='' alt='company,logo' /> symbol"
    }

    {
        "_id":4,
        "name": "Innovative Software pvt. limited 4",
        "description": "This is a software, company with,<img src='' class='' alt='company, logo' /> symbol"
    }

现在我想要一个正则表达式来查找所有公司 描述字段满足以下条件-

1- 逗号和其后的字母/数字/图像之间没有空格。
2- 它不应该包含在 img 标签中写入的内容.

所以在我的情况下,我想要以下文档的输出-
_id:2("description": "这是一个软件e,c公司...,

_id:4("description": "这是一个软件,公司机智h,..

我想要一个类似的查询-

db.Companies.find({description:{$regex:'regular expression'}})

是否可以在查询本身中实现,或者我需要在代码本身中编写逻辑。我正在使用 pymongo。

【问题讨论】:

  • 你可以使用 JSONPATH 代替
  • Sujith - 如何使用它?
  • 苏吉特谢谢!但似乎它需要添加一个库。我想在不使用任何额外库的情况下实现它。
  • 你的第二个条件是什么(它不应该包括写在标签内的内容。)?

标签: javascript regex mongodb pymongo


【解决方案1】:

试试这个正则表达式:

/"description"\s*:\s*"(?:[^<'"]|\\")+?,(?=[<a-z]).+"/gi

说明

演示

http://regex101.com/r/bN3uY7

【讨论】:

  • 谢谢亚历克斯!你很酷!! :)
猜你喜欢
  • 1970-01-01
  • 2017-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-25
  • 2011-11-16
  • 1970-01-01
  • 2011-11-20
相关资源
最近更新 更多