正则表达式匹配一系列点之前的任何内容

时间:2021-03-17 20:12:29

I'd like to make a regex that matches anything that is succeeded by four points or more:

我想制作一个匹配任何由四点或更多点成功的东西的正则表达式:

asdf....... 

Would match to asdf.

会匹配asdf。

I've tried with:

我尝试过:

.+?(?=\.{4,})

but it only discards the last four dots, so it matches asdf....

但它只丢弃最后四个点,所以它匹配asdf ....

How can it be done?

如何做呢?

2 个解决方案

#1


3  

The .+?(?=\.{4,}) regex matches asdf in asdf....... since it finds 4 or more dots right after the value, but since the \.{4,} is inside a non-consuming pattern, the ....... remains to be checked and the first . in that substring is matched again since .+? matches any 1 or more chars other than line break chars, but as few as possible. Same happens with the second and third .s since they all are followed with 4+ commas.

。+?(?= \。{4,})正则表达式在asdf .......中匹配asdf,因为它在值后面找到4个或更多点,但是因为\。{4,}位于a非消费模式,.......仍有待检查和第一个。在那个子串中再次匹配。+?匹配除换行符之外的任何1个或多个字符,但尽可能少。同样的情况发生在第二个和第三个.s,因为它们都跟着4个以上的逗号。

What you may do is either make the dot matching part consuming and capture the .+? (then the value you need will be in Group 1):

您可能要做的是使点匹配部分消耗并捕获。+? (那么您需要的值将在第1组中):

(.+?)\.{4,}

See the regex demo

请参阅正则表达式演示

Here, (.*?) is a capturing group matching 0+ chars (use * instead of + to match 1 or more) other than line break chars and \.{4,} will match and consuming 4 or more . chars (not allowing to check for a match when inside the dots).

这里,(。*?)是一个捕获组,匹配0+字符(使用*而不是+匹配1或更多)除了换行符和\。{4,}将匹配并消耗4或更多。 chars(不允许在点内部检查匹配)。

#2


0  

^(?!\.+)(.+?)\.{4,}$

Captures anything preceding 4 or more dots, but also makes sure that the string isn't all dots

捕获4个或更多点之前的任何内容,但也确保该字符串不是所有点

If you're searching through a larger document:

如果您正在搜索更大的文档:

(?!\.).+?(?<!\.)(?=\.{4,})

See here for the first example

请参阅此处的第一个示例

See here for the second example

请参阅此处以获取第二个示例

#1


3  

The .+?(?=\.{4,}) regex matches asdf in asdf....... since it finds 4 or more dots right after the value, but since the \.{4,} is inside a non-consuming pattern, the ....... remains to be checked and the first . in that substring is matched again since .+? matches any 1 or more chars other than line break chars, but as few as possible. Same happens with the second and third .s since they all are followed with 4+ commas.

。+?(?= \。{4,})正则表达式在asdf .......中匹配asdf,因为它在值后面找到4个或更多点,但是因为\。{4,}位于a非消费模式,.......仍有待检查和第一个。在那个子串中再次匹配。+?匹配除换行符之外的任何1个或多个字符,但尽可能少。同样的情况发生在第二个和第三个.s,因为它们都跟着4个以上的逗号。

What you may do is either make the dot matching part consuming and capture the .+? (then the value you need will be in Group 1):

您可能要做的是使点匹配部分消耗并捕获。+? (那么您需要的值将在第1组中):

(.+?)\.{4,}

See the regex demo

请参阅正则表达式演示

Here, (.*?) is a capturing group matching 0+ chars (use * instead of + to match 1 or more) other than line break chars and \.{4,} will match and consuming 4 or more . chars (not allowing to check for a match when inside the dots).

这里,(。*?)是一个捕获组,匹配0+字符(使用*而不是+匹配1或更多)除了换行符和\。{4,}将匹配并消耗4或更多。 chars(不允许在点内部检查匹配)。

#2


0  

^(?!\.+)(.+?)\.{4,}$

Captures anything preceding 4 or more dots, but also makes sure that the string isn't all dots

捕获4个或更多点之前的任何内容,但也确保该字符串不是所有点

If you're searching through a larger document:

如果您正在搜索更大的文档:

(?!\.).+?(?<!\.)(?=\.{4,})

See here for the first example

请参阅此处的第一个示例

See here for the second example

请参阅此处以获取第二个示例