Thread overview | |||||
---|---|---|---|---|---|
|
September 16, 2017 Convert user input string to Regex | ||||
---|---|---|---|---|
| ||||
Hi, Is there a way to transform user input string to a regular expression? For example, I want to write a `grep`-like program ``` mygrep -E '/pattern/i' file.txt ``` and here the user's parameter `/pattern/i` would be converted to a Regex object. Fyi, in Ruby, `to_regexp` is a useful gem: https://rubygems.org/gems/to_regexp Thanks a lot. |
September 16, 2017 Re: Convert user input string to Regex | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ky-Anh Huynh | On Saturday, 16 September 2017 at 03:18:31 UTC, Ky-Anh Huynh wrote: > Is there a way to transform user input string to a regular expression? For example, I want to write a `grep`-like program import std.regex; auto re = regex(user_pattern, user_flags); You'll probably want to split it on the '/' to split the pattern and the flags since they are two separate variables to the regex function, but that's all you need to do. http://dpldocs.info/experimental-docs/std.regex.regex.2.html |
September 17, 2017 Re: Convert user input string to Regex | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Saturday, 16 September 2017 at 03:23:14 UTC, Adam D. Ruppe wrote:
> On Saturday, 16 September 2017 at 03:18:31 UTC, Ky-Anh Huynh wrote:
>> Is there a way to transform user input string to a regular expression? For example, I want to write a `grep`-like program
>
> import std.regex;
>
> auto re = regex(user_pattern, user_flags);
>
>
> You'll probably want to split it on the '/' to split the pattern and the flags since they are two separate variables to the regex function, but that's all you need to do.
>
> http://dpldocs.info/experimental-docs/std.regex.regex.2.html
Thanks Adam. I will give it a try.
|
Copyright © 1999-2021 by the D Language Foundation