Hi
How can I convert a regex-string including /.../ to a Regex?
e.g. '/abc/i' => /abc/i
thank you
Opti
Hi
How can I convert a regex-string including /.../ to a Regex?
e.g. '/abc/i' => /abc/i
thank you
Opti
do you know that it's going to be a well-formed regex representation? (i.e.
always in a form that eval(str) will return a regex?)
if so, i'd go with splitting on "/", extracting the parts, and passing them
as arguments to Regexp.compile()
martin
On Mon, Apr 4, 2022 at 2:11 PM Die Optimisten <inform@die-optimisten.net> wrote:
Hi
How can I convert a regex-string including /.../ to a Regex?
e.g. '/abc/i' => /abc/i
thank you
OptiUnsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Thank you - regex normally should be ok,
but can be more complicated. Because of possible \/ in the middle,
splitting is not very nice...
Why is there no .parse ?
Opti
Am 04.04.22 um 23:14 schrieb Martin DeMello:
do you know that it's going to be a well-formed regex representation?
(i.e. always in a form that eval(str) will return a regex?)if so, i'd go with splitting on "/", extracting the parts, and passing
them as arguments to Regexp.compile()martin
On Mon, Apr 4, 2022 at 2:11 PM Die Optimisten > <inform@die-optimisten.net <mailto:inform@die-optimisten.net>> wrote:
Hi
How can I convert a regex-string including /.../ to a Regex?
e.g. '/abc/i' => /abc/i
thank you
OptiUnsubscribe: <mailto:ruby-talk-request@ruby-lang.org
<mailto:ruby-talk-request@ruby-lang.org>?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
string = '/abc/I'
eval(string) #=> /abc/I
# or
pattern, options = string.match(%r{/(.*)/(.+)}).captures
Regexp.new(pattern, options) #=> /abc/i
…However, any such approach is a bit dodgy! It would have been better the original string was a well-formed string interpretation of the regex, i.e.
/abc/i.to_s == '(?i-mx:abc)'
Regexp.new("(?i-mx:abc)”) #=> /(?i-mx:abc)/
On 4 Apr 2022, at 22:10, Die Optimisten <inform@die-optimisten.net> wrote:
Hi
How can I convert a regex-string including /.../ to a Regex?
e.g. '/abc/i' => /abc/i
thank you
OptiUnsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
El dl, 4 abr 2022 a les 23:33 Tom Lord
…However, any such approach is a bit dodgy! It would have been better the
original string was a well-formed string interpretation of the regex, i.e.
/abc/i.to_s == '(?i-mx:abc)'
Indeed, a regular expression does not have delimiters. A regular expression
is what goes between them in literals like /.../, %r{...}, etc.
I've required the notation (?i-mx:abc) in some programs that had regexps as
configuration. That is, you configure a regular expression proper.
That email is missing some more text.
A regular expression does not have delimiters, literals do, different.
So, in /foo.*bar/, the regular expression is
foo.*bar
the slashes are not part of the regular expression. Same if it was
%r{foo.*bar}.
In some programming languages there are no regular expression literals,
they just pass the regular expression in a string.
So, if you need to get a regular expression as input, strings with regular
expressions like "(?i-mx:abc)" are the way to go.