If i use
.gsub(/matchthisregex/,"replace_with_this")
does gsub store what it matches with the regex somewhere? I'd like to use what it matches in my replacement string. For example something like
"replace_with_" + matchedregexstring + "this"
in my above example where the matchedregexstring would be the stored match from gsub? Sorry if that was confusing, I don't know how else to word that.
gsub (s, pattern, repl [, n]) Returns a copy of s in which all (or the first n , if given) occurrences of the pattern have been replaced by a replacement string specified by repl , which can be a string, a table, or a function. gsub also returns, as its second value, the total number of matches that occurred.
gsub() function replaces all matches of a string, if the parameter is a string vector, returns a string vector of the same length and with the same attributes (after possible coercion to character). Elements of string vectors which are not substituted will be returned unchanged (including any declared encoding).
Regular expressions (shortened to regex) are used to operate on patterns found in strings. They can find, replace, or remove certain parts of strings depending on what you tell them to do.
A regular expression is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings. Ruby regular expressions i.e. Ruby regex for short, helps us to find particular patterns inside a string. Two uses of ruby regex are Validation and Parsing.
From the fine manual:
If replacement is a
String
it will be substituted for the matched text. It may contain back-references to the pattern’s capture groups of the form\d
, where d is a group number, or\k<n>
, where n is a group name. If it is a double-quoted string, both back-references must be preceded by an additional backslash. However, within replacement the special match variables, such as&$
, will not refer to the current match.
[...]
In the block form, the current match string is passed in as a parameter, and variables such as$1
,$2
, $`,$&
, and$'
will be set appropriately. The value returned by the block will be substituted for the match on each call.
If you don't care about capture groups (i.e. things like (expr)
in the regex) then you can use the block form and $&
:
>> 'where is pancakes house?'.gsub(/is/) { "-#{$&}-" } => "where -is- pancakes house?"
If you do have capture groups then you can use \n
in the replacement string:
>> 'where is pancakes house?'.gsub(/(is)/, '-\1-') => "where -is- pancakes house?"
or $n
in the block:
>> 'where is pancakes house?'.gsub(/(is)/) { "-#{$1}-" } => "where -is- pancakes house?"
I found out here that gsub's matches could actually be accessed through the Regexp.last_match
variable (class MatchData) like this:
my_string.gsub(my_regexp){ Regexp.last_match.inspect }
To give a more practical example, if you want to log all matches, it may be used as follows:
"Hello world".gsub(/(\w+)/) { Regexp.last_match.captures.each{ |match| Rails.logger.info "FOUND: #{match}"} } #Rails log: FOUND: Hello FOUND: world
In your specific case, you could do something like this:
mystring.gsub(/(matchthisregex)/){ mystring = "replace_with_#{Regexp.last_match[0].to_s}this"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With