Add R to dynamically generated regex pattern

I would like to create a regex pattern dynamically. I can build a string containing a template. Now in the type definition

  std::regex pattern{ R"((\w)+)" };

      

"((\w)+)"

there should be my string containing the template I am creating.

But how can I add R

outside of the line?

I could create a string using escape characters. But I'm curious if there is a way around this.

Let's pretend that

std::string myPatternWithoutEscapeChar;

      

is my string containing a regex with no escape characters like ([\]+)

, not([\\]+)

I would like to do what amounts to

  std::regex pattern{ R... };

      

with this R

as in the first definition above, and where ...

is the content myPatternWithoutEscapeChar

inside ""

.

+3


source to share


1 answer


It doesn't matter if you use the Raw string construct or not.
Its value is passed to the regular expression engine, which is honored.

So, as always, there is a language string parsing phase and then a regular expression parsing phase.
Therefore, it is very important to first write and debug the entire regex in its original state.
This avoids confusion.

Use a regex generator / test tool. Nice RegexFormat 5 .
It's like a Swiss army knife for handling regular expressions. It also has regular motors built into a
complete test harness. Find / replace paradigm. Formats / compresses, checks for Eror, and will make any type of string out of it, including raw, which you can put into your source code. It
can also take lines of source code, parse them for a language, then for a regular expression, and then process the regular expression.

Your only concern is the regular expressions, which you should learn.

The first lesson is that a regular expression is a language, it contains combinations of metacharacter constructs.

Sample Metashar .,?,\,+,*,^,$,#,[,],(,)

They all have special meaning depending on how they are used. A construct can be a series of metachars / normal characters that start to end it,
example(?'Var' ... )

As with all languages, there must be a way to introduce literals into code constructs.
A conflict develops if the matched literal is a metacharacter.

To distinguish this, it has a literal meaning, an escape character is placed in front of it.

But what happens when the letter matching is the actual escape character?
This, the escape character actually escaped, now it has a literal meaning.



You really don't want to guess what regex looks like engines below the original representation.
The raw representation is devoid of language or regular expression restrictions.

For example, you specify ([\]+) as opposed to ([\\]+)

It ([\]+)

will not compile in its raw form into a regular expression object.
It has an opening [

meta \]

-bar literal meta-bar without a private meta-bar ]

.

This one ([\\]+)

better has an opener [

with one literal \

, then a close ]

.

So ([\\]+)

this is a RAW regex.

It is then presented to the language as a double-quoted string "([\\\\]+)"


or as a raw string R"([\\]+)"

.

I just took a look at the new C ++ 11 Raw string constructors, I know you can use any character series, so this is just general information.

Good luck!

+1


source







All Articles