Calling all #regex witches and warlocks!
I want to match individually each space at the beginning of each line of the input text. I do not want to match any other space.
Text input:
Baseline of this text
Indentation with two spaces
Surprise, the text has two spaces too!
Target syntax: PCRE2 (PHP >= 7.3)
Expected matches: 6
So far my best attempt is /(?<=^| ) /m
but it also matches spaces beyond the first in any series of two or more consecutive spaces anywhere in the text, not just at the beginning of each line. See https://regex101.com/r/GXfJL0/1
Can you do better?
regex101: build, test, and debug regex
Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET.regex101
Ratwave :shrimpred: β
•/(?<=^|(?<![^\s]) ) /mg
https://regex101.com/r/SEAu43/1
regex101: build, test, and debug regex
regex101Hypolite Petovan likes this.
Rasmus Fuhse
•Oriel Jutty :hhHHHAAAH:
•/(?:^|\G) /mg
, but there's probably a non-stupid way to do whatever you're trying to achieve.Hypolite Petovan likes this.
Hypolite Petovan
•\G
!Hypolite Petovan
•\G
.Rokosun
•/^ +/mg
Hypolite Petovan
•Rokosun
•Hypolite Petovan
•@Rokosun I was given the final clue by @Locrian Filth in https://infosec.exchange/@barubary/110099448284247487 with the
\G
macro that matches the previous match. This way I can match spaces at the start of a line, and then all the spaces after who are individually matched in order thanks to\G
. Other strings of consecutive spaces aren't matched because the first space in the string isn't matched.Final regular expression:
/(?<=^|\G) /m
Oriel Jutty :hhHHHAAAH:
2023-03-28 06:17:37
Raroun likes this.
Martijn Vos
•Hypolite Petovan
•Martijn Vos
•Hypolite Petovan likes this.
Hypolite Petovan
•