[Ruby] Exclude and replace specific patterns with regular expressions

background

--http: // aaa / ccc http: // aaa / bbb / ccc --However, you can leave the http: // aaa / bbb / ccc as it is.

I wanted a command that could be used in that situation. In other words, it is an exclusion that there are patterns that replace, but exclude everything instead of replacing it.

command

Use regular expression negative look-ahead

For example, if you don't replace only the past system of play,

"play plays played".gsub(/play(?!ed)/, "eat")
#=> "eat eats played"

It becomes the command. Regarding the above url,

"http://aaa/ccc".gsub(/http:\/\/aaa\/(?!bbb\/)/, "http://aaa/bbb/")
#=>"http://aaa/bbb/ccc"

"http://aaa/bbb/ccc".gsub(/http:\/\/aaa\/(?!bbb\/)/, "http://aaa/bbb/")
# => "http://aaa/bbb/ccc"

Recommended Posts

[Ruby] Exclude and replace specific patterns with regular expressions
Switch beans with @ConditionalOnExpression and SpEL regular expressions
Distinguish between integers and decimals with regular expressions
colorize and regular expressions
About regular expressions in Ruby
Handling of line beginning and line ending in regular expressions in Ruby
Parse Japanese addresses with regular expressions
With ruby ● × Game and Othello (basic review)
Easily make troublesome regular expressions with Rubular
Easy to trip with Java regular expressions
Convert JSON to TSV and TSV to JSON with Ruby
Regular expressions
[Ruby] Arguments with keywords and default values of arguments
About Ruby regular expressions (metacharacters, captures, related methods)
I implemented Ruby with Ruby (and C) (I played with builtin)
Try to link Ruby and Java with Dapr
Create jupyter notebook with Docker and run ruby
Solving with Ruby and Crystal AtCoder ABC 129 D
[Ruby] Exclude duplicate elements with the uniq method.
About regular expressions used in ruby sub method