Skip to content

Instantly share code, notes, and snippets.

@draegtun
Last active December 17, 2015 08:28
Show Gist options
  • Save draegtun/5580067 to your computer and use it in GitHub Desktop.
Save draegtun/5580067 to your computer and use it in GitHub Desktop.
Rebol []
; PHP
; $string = preg_replace( '/^@(.*?)@$/', '#$1#', $string );
;
; Perl
; $string =~ s/^\@(.*?)\@$/#$1#/;
;
; # or...
; $string =~ s/\A\@(.*?)\@\Z/#$1#/;
string: "@one and @two and three@"
; basic parse match
if [parse/all string ["@" some [to "@" skip] end]] [probe string]
; return copy with changes (though here we've copied back to string !!)
parse/all string [
"@" start: some [to "@" fin: skip] end
(string: rejoin ["#" copy/part start fin "#"])
]
probe string
; in place change (changing it bback to what it was)
parse/all string [
start: "#" some [to "#" fin: skip] end
(change start "@" change fin "@")
; a DRYer but longer alternative to above line...
;(foreach chg compose [(start) (fin)] [change chg "@"])
]
probe string
; => "@one and @two and three@"
; => "#one and @two and three#"
; => "@one and @two and three@"
Rebol []
some-text: [
"nothing here"
"@start but not end"
"end but not start@"
"@nope no @this one"
"@Do this line@"
"@yes with @three lots of ats@"
]
; print anything that matches /^@(.*?)@$/ (ie. just the last two lines!)
foreach string some-text [
parse/all string [
#"@"
start: some [to #"@" fin: skip]
end
(print copy/part start fin) ; if parsed true then print captured text
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment