简体   繁体   English

正则表达式搜索有空间还是无空间?

[英]regex to search for with space or without space in chain?

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 硕士项目建设项目学校项目第一阶段学校项目第二阶段SchoolProject

Right now I have the following as my regex. 现在我有以下作为我的正则表达式。

(^Master Project|^Building Project|^School Project ([]AZ)) (^ Master Project | ^ Building Project | ^ School Project([] AZ))

This works on everything EXCEPT on the single word schoolproject. 这适用于单个单词schoolproject上的所有内容。 I tried adding in ? 我尝试添加? between school and Project after the white space but that did not seem to help. 在学校和Project之间留出空白,但这似乎没有帮助。 I tried a lookaround and lookaround negative but I am not really able to get it to work with either method. 我尝试了环视和环视否定,但我真的无法使其与任何一种方法一起使用。

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, eg 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 http://regexr.com/3ceno

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

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