Regular Expressions 101

Community Library Entry

1

Regular Expression
PCRE2 (PHP >=7.3)

/
^[#].*\n*(*SKIP)(*F)| # Version 1 (substitution: '$1$2\n'): # Match 1-80 characters, backtracking as necessary until whitespace or # EOL is reached. If words > 80 characters (w/o whitespace) are found, # no wrapping is performed on that word. # [1353 steps] #(.{1,80})(?:\s|$)|(\S{81,})(?:\s|$) # Version 2 (substitution: '$1$2\n'): # Same as Version 1 but with a negative look-ahead at the front to skip # to the alternate branch sooner. # [773 steps] #(?!\S{81})(.{1,80})(?:\s|$)|(\S{81,})(?:\s|$) # Version 3 (substitution: '$1$2\n'): # Same as Version 1 but alternations swapped. # [749 steps] #(\S{81,})(?:\s|$)|(.{1,80})(?:\s|$) # Version 4 (substitution: '$1\n'): # Combine alternations into one group. # [712 steps] (\S{79,}|.{1,80})(?:\s|$)
/
gmx

Description

Four examples of word wrapping at the 80 character mark, each one taking fewer steps than the last.

Comment and uncomment lines with # to see the difference between versions. Optimization discussions welcome!

Submitted by OnlineCop - 7 years ago (Last modified a year ago)