Reputation: 249
I couldn't find any proper answer as Regexp stands for Regular Expressions. Here is an example:
test = Regexp.new("Hello")
It returns /Hello/
, but I'm confused over the use of this. Why not use /Hello/
directly? Can anyone give me a small example of Regexp.new()
function?
Upvotes: 3
Views: 803
Reputation: 17020
All constructor features are accessible to the literals. Actually, literals allow you to specify even more options than new
:
# Supported by Regexp.new
/regexp/m => Regexp::MULTILINE
/regexp/i => Regexp::IGNORECASE
/regexp/x => Regexp::EXTENDED
/regexp/n => Regexp::NOENCODING
# Apparently only available to literals
/regexp/u => UTF-8
/regexp/s => Windows-31J
/regexp/e => EUC-JP
/regexp/o => Interpolate only once; only makes sense in literals
Why are there many encoding options? The reason is that regular expression objects created via literals assume the encoding of the source code; the encoding options exist to force a specific encoding. The constructor lets you pass in an arbitrary string whose encoding you can explicitly control.
The new
method is very useful when the regular expression is of dynamic nature. While it is possible to interpolate strings into the literal:
string = '.+'
regexp = /#{string}/
It is not possible to interpolate options into the literal. There is nothing like this:
options = 'ui'
regexp = /.+/#{options}
Using the constructor here is advantageous because it lets you decide which options to pass in dynamically. If you were to make a program that lets users create their own regular expressions via a graphical user interface, it would be natural to create regular expressions using Regexp.new
.
Upvotes: 3
Reputation: 33506
Let's imagine you want to make your system detect "a sentence that starts with DOG and ends with CAT". Obvious Regex would be something like /DOG.+CAT\./
[1].
So far, works. But now you'd like to be able to enhance the system, so the user can configure the opening and closing words. You might be able to do it with //
syntax and interpolation:
/#{startWord}.+#{endWord}\./
but you also can do it with simple strings:
Regex.new(startWord + ".+" + endWord + "\.")
Cosmetics. Use what you like the most. However, there is one more thing that's important. With the new
function, you can pass not only one, but also more parameters. See the docs.
So, the main[2] purpose of the existence of new
from your point is actually being able to pass more arguments than just one. With //
you can only pass the regex-formula[3]. With new
you can pass options.
Few details were oversimplified for clarity.
[1] It is not a valid "sentence detector". It is only some regex to show the problem.
[2] It's not main purpose. The new
is a constructor. It must exist or we would not be able to construct the object. Actually, the /blargh/
syntax is translated into a new
call.
[3] Actually, with //
you can pass some options too, like /CaSEiNseNsiTive/i
. But, well, constructors are still more flexible.
Upvotes: 4
Reputation: 356
Imagine you have a variable with a string which you want to reuse in a regular expression:
a_string = "I want to match this"
You can then easily create a regex using:
Regexp.new(a_string)
Upvotes: 3