Regular expressions: Difference between revisions

Content added Content deleted
(regular expressions in Common Lisp using CL-PPCRE)
(Added R code)
Line 445: Line 445:
string = re.sub(" a "," another ",string)
string = re.sub(" a "," another ",string)
print string</lang>
print string</lang>

=={{header|R}}==
First, define some strings.
<lang R>
pattern <- "string"
text1 <- "this is a matching string"
text2 <- "this does not match"
</lang>
Matching with grep. The indices of the texts containing matches are returned.
<lang R>
grep(pattern, c(text1, text2)) # 1
</lang>
Matching with regexpr. The positions of the starts of the matches are returned, along with the lengths of the matches.
<lang R>
regexpr(pattern, c(text1, text2))
</lang>
[1] 20 -1
attr(,"match.length")
[1] 6 -1
Replacement
<lang R>
gsub(pattern, "pair of socks", c(text1, text2))
</lang>
[1] "this is a matching pair of socks" "this does not match"


=={{header|Raven}}==
=={{header|Raven}}==