Jump to content

Regular expressions: Difference between revisions

no edit summary
No edit summary
Line 63:
$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)
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.