正则表达式搜索空间或没有空间在链吗? - regex to search for with space or without space in chain?

- 此内容更新于:2015-12-20
主题:

我想写一个搜索的正则表达式的一系列项目。然而,其中的一个项目,我使用搜索不遵循一个标准命名约定在我有问题的巩固我的正则表达式搜索足够小。这是一个例子。我已经掌握项目建设项目学校项目第一阶段项目第二阶段SchoolProject现在我有下面的正则表达式。(主项目|^^建筑工程|^学校项目([]a-z))这是在除了schoolproject单个词。我试着添加?学校和项目后之间空白但这似乎并没有帮助。我试着看看,看看-但我不能够得到它使用方法。

原文:

I am trying to write a regex that searches for a series of items. However, One of the projects that I am using the search to find does not follow a standard naming convention all the way through so I am having issues consolidating my regex search small enough.

Here is an example.

I have

Master Project Building Project School Project Phase 1 School Project Phase 2 SchoolProject

Right now I have the following as my regex.

(^Master Project|^Building Project|^School Project ([]A-Z))

This works on everything EXCEPT on the single word schoolproject. I tried adding in ? between school and Project after the white space but that did not seem to help. I tried a lookaround and lookaround negative but I am not really able to get it to work with either method.

网友:应该工作……regexr.com/3cenl

(原文:(School ?Project) should work... regexr.com/3cenl)

解决方案:
假设你的“项目名称”都在一行上所示的例子:这也认为“阶段”后剩下的名字,例如学校项目第一阶段,而不是第1阶段学校项目。如果每个“项目名称”在自己的字符串,您可以添加在“主”之前,“建筑”等,http://regexr.com/3ceno
原文:

Assuming your "project names" are all on one line as shown in the example:

(Master Project|Building Project|School ?Project([ A-Za-z]* [0-9])?)

This also assume that the "Phases" come after the rest of the name, e.g. School Project Phase 1, not Phase 1 School Project.

If each "project name" is in its own string, you can add back in the ^ before "Master", "Building", etc.

http://regexr.com/3ceno