I’ll be brief as I can. I’m trying to use preg_replace’s regex to find a digit, but I want to non destructively edit the string.
an example: (albeit this is an approximation due to data protection)
$subject_string = 'section 1.1: Disability ........' ; $outcome = preg_replace( '/$section[d.d]+/' , '<hr/>' , $subject_string ); // $outcome will be: "<hr/>section 1.1: Disability ........"
Any help would be gratefully received
Advertisement
Answer
Use
bsections*d+(?:.d+)*:
Replace with <hr/>$0
. See regex proof.
EXPLANATION
-------------------------------------------------------------------------------- b the boundary between a word char (w) and something that is not a word char -------------------------------------------------------------------------------- section 'section' -------------------------------------------------------------------------------- s* whitespace (n, r, t, f, and " ") (0 or more times (matching the most amount possible)) -------------------------------------------------------------------------------- d+ digits (0-9) (1 or more times (matching the most amount possible)) -------------------------------------------------------------------------------- (?: group, but do not capture (0 or more times (matching the most amount possible)): -------------------------------------------------------------------------------- . '.' -------------------------------------------------------------------------------- d+ digits (0-9) (1 or more times (matching the most amount possible)) -------------------------------------------------------------------------------- )* end of grouping -------------------------------------------------------------------------------- : ':'
Code snippet:
$re = '/bsections*d+(?:.d+)*:/'; $str = 'section 1.1: Disability ........'; $subst = '<hr/>$0'; $result = preg_replace($re, $subst, $str); echo "The result of the substitution is ".$result;