Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl6 getting a regex from a string

Tags:

regex

raku

I want to put a regex in a YAML config file (say: config/filename.yaml), eg.

-
  section: Begining
  regex: '/ ^ <[ a..b, A..B ]> /'
-
  section: Next
  regex: '/ ^ <[ c..z, C..Z ]> /'

When I read this into a hash (eg. using YAMLish), eg.,

use YAMLish;
my @h = load-yaml('config/filename.yaml'.IO.slurp);

naturally I have a string in @h[0]<regex>

So how do I recover a regex from a string for use in a match?

I want something like the following, but the following doesn't work:

say 'Its a beginning' if 'A beginning' ~~ @h[0]<regex>

It doesn't work as desired because @h[0]<regex> is a Str, so the smart match is testing a Str in @h[0]<regex> against a Str literal. So how to get the regex out of the Str?

like image 361
Richard Hainsworth Avatar asked Nov 24 '18 14:11

Richard Hainsworth


People also ask

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.

How do I find a character in a string in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

What is a Perl regular expression?

Summary: in this tutorial, you are going to learn about Perl regular expression, the most powerful feature of the Perl programming language. A regular expression is a pattern that provides a flexible and concise means to match the string of text. A regular expression is also referred to as regex or regexp.

How to use negated regex operator in Perl?

In Perl, regular expression negated regex operator is used to checking the string is not equal to the regular expression which was specified on the right-hand side. Below is the example are as follows.

Why does the whole expression return a value in regex?

The whole expression returns a value to indicate whether the regular expression regex was able to match the string successfully. Let’s take a look at an example. First, we declare a string variable: Second, to find if the string $s contains the substring ul you use the following regular expression: Putting it all together.

How to define the pattern of string or character in Perl?

Perl regular expression is used to define the pattern of string or character in Perl. Using regular expression in Perl we have to define the pattern of any string or character. The basic method is available to apply regular expression in Perl is to use the pattern binding operator =~ and !~, the first operator is the assignment operator.


Video Answer


1 Answers

You interpolate a string held in a variable $var into a regex via / <$var> /, or, in case of more complex expressions like yours, / <{ @h[0]<regex> }> /.

Note, however, that you first have to remove the enclosing slashes from your string. For trusted input, there's of course always EVAL...

like image 179
Christoph Avatar answered Oct 22 '22 23:10

Christoph