Regular expressions: Difference between revisions
Content added Content deleted
m (→{{header|Ruby}}: use lang tag) |
|||
Line 431: | Line 431: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Test |
Test |
||
string="I am a string" |
<lang ruby> string="I am a string" |
||
puts "Ends with 'string'" if string[/string$/] |
puts "Ends with 'string'" if string[/string$/] |
||
puts "Does not start with 'You'" if !string[/^You/] |
puts "Does not start with 'You'" if !string[/^You/]</lang> |
||
Substitute |
Substitute |
||
puts string.gsub(/ a /,' another ') |
<lang ruby> puts string.gsub(/ a /,' another ') |
||
#or |
#or |
||
string[/ a /]='another' |
string[/ a /]='another' |
||
puts string |
puts string</lang> |
||
Substitute using block |
Substitute using block |
||
puts(string.gsub(/\bam\b/) do |match| |
<lang ruby> puts(string.gsub(/\bam\b/) do |match| |
||
puts "I found #{match}" |
puts "I found #{match}" |
||
#place "was" instead of the match |
#place "was" instead of the match |
||
"was" |
"was" |
||
end) |
end)</lang> |
||
=={{header|Slate}}== |
=={{header|Slate}}== |