Regular expressions: Difference between revisions
Content added Content deleted
m (→[[Java]]) |
No edit summary |
||
Line 63: | Line 63: | ||
$string =~ s/i/u/ig; # would change "I am a string" into "u am a strung" |
$string =~ s/i/u/ig; # would change "I am a string" into "u am a strung" |
||
==[[Ruby]]== |
|||
Test |
|||
string="I am a string" |
|||
puts "Ends with 'string'" if string[/string$/] |
|||
puts "Does not start with 'You'" if !string[/^You/] |
|||
Substitute |
|||
puts string.gsub(/ a /,' another ') |
|||
#or |
|||
string[/a/]='another' |
|||
puts string |
|||
Substitute using block |
|||
puts(string.gsub(/\bam\b/) do |match| |
|||
puts "I found #{match}" |
|||
#place "was" instead of the match |
|||
"was" |
|||
end) |