Regular expressions: Difference between revisions
Content added Content deleted
No edit summary |
(Add Brat example of regular expressions) |
||
Line 270: | Line 270: | ||
After the last number, the match expression fails. |
After the last number, the match expression fails. |
||
=={{header|Brat}}== |
|||
Test |
|||
<lang brat>str = "I am a string" |
|||
true? str.match(/string$/) |
|||
{ p "Ends with 'string'" } |
|||
false? str.match(/^You/) |
|||
{ p "Does not start with 'You'" } |
|||
</lang> |
|||
Substitute |
|||
<lang brat># Substitute in copy |
|||
str2 = str.sub(/ a /, " another ") |
|||
p str # original unchanged |
|||
p str2 # prints "I am another string" |
|||
# Substitute in place |
|||
str.sub!(/ a /, " another ") |
|||
p str # prints "I am another string" |
|||
# Substitute with a block |
|||
str.sub! /a/ |
|||
{ match | match.upcase } |
|||
p str # prints "I Am Another string" |
|||
</lang> |
|||
=={{header|C}}== |
=={{header|C}}== |