perl - Substring replacement using regex -
i having terrible time learning perl regular expressions. trying :
- replace occurrences of single
#
@ beginning of line with:#####
. - replace occurrences of full line of
#
characters (ignoring leading or trailing spaces) with
# ---------- #
.
i know s/#
that's know , can find. suggestions.
the beginning of line matched ^
. therefore, line starting #
matched by
/^#/
if want #
single, i.e. not followed #
, must add negative character class:
/^#[^#]/
we not want replace character following #
, replace non matching group (called negative look-ahead):
/^#(?!#)/
to add replacement, change
s/^#(?!#)/#####/
the full line can matched following regular expression:
/^#+$/
plus means "once or more", ^
, $
have been explained. have ignore leading , trailing spaces (*
means "zero or more"):
/^ *#+ *$/
we not want spaces replaced, have keep them. parentheses create "capture groups" numbered 1:
s/^( *)#+( *)$/$1# ---------- #$2/
Comments
Post a Comment