Regex matches everything that precedes a sequence of points

I would like to create a regular expression that matches all that is done with four points or more:

asdf....... 

Complies with asdf .

I tried:

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

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

How can I do that?

+5
source share
2 answers

The regular expression .+?(?=\.{4,}) matches asdf in asdf....... , because it finds 4 or more points immediately after the value, but since \.{4,} is inside the template , not consuming, ....... remains to check the first . in this substring is the same again, since .+? matches any 1 or more characters other than line breaks, but as small as possible. The same thing happens with the second and third . as they are followed by 4 + commas.

What you can do is to either make part of the correspondence to the consuming points and capture .+? (then the value you need will be in group 1):

 (.+?)\.{4,} 

Watch the regex demo

Here (.*?) Is the capture group corresponding to 0+ characters (use * instead of + to match 1 or more), except for line break characters, and \.{4,} will match and consume 4 or more . (not allowing you to check compliance when inside dots).

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

Captures everything that precedes 4 or more points, but also ensures that the line does not have all points

If you are viewing a larger document:

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

See here the first example

See second example here

0
source

Source: https://habr.com/ru/post/1275054/


All Articles