data.to_enum(:scan,/(#entity[0])/i).map do |m,|
p $`.size
How can I use dynamic variable in regular expression?
#entity[0]
returns a value, but in the above syntax #entity[0] is taken literally in the regex.
String Interpolation, it is all about combining strings together, but not by using the + operator. String Interpolation works only when we use double quotes (“”) for the string formation. String Interpolation provides an easy way to process String literals.
Two uses of ruby regex are Validation and Parsing. Ruby regex can be used to validate an email address and an IP address too. Ruby regex expressions are declared between two forward slashes. This will return the index of first occurrence of the word 'hi' if present, or else will return ' nil '.
These interpolations are wrapped in ${} , such as ${var. foo} . The interpolation syntax is powerful and allows you to reference variables, attributes of resources, call functions, etc. You can also perform simple math in interpolations, allowing you to write expressions such as ${count.
String interpolation is a technique that enables you to insert expression values into literal strings. It is also known as variable substitution, variable interpolation, or variable expansion. It is a process of evaluating string literals containing one or more placeholders that get replaced by corresponding values.
You want /#{entity[0]}/i
. #{}
is the syntax for variable insertion in strings and regexes.
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