返回std.regex.regex的值? [英] Return value of std.regex.regex?
问题描述
我正在尝试编写一个函数,该函数需要一个输入字符串,一个正则表达式(由 std.regex.regex
由原始字符串制成)和一个错误消息字符串,并尝试使用正则表达式匹配输入字符串中的内容,如果没有匹配项,则显示错误消息。到目前为止,我想到了以下签名:
I'm trying to write a function that takes an input string, a regex (made by std.regex.regex
from a rawstring) and an error message string, and attempt to match something from the input string using the regex, displaying the error message if there are no matches. I came up with the following signature so far:
string check_for_match (string input, Regex r, string error_message)
但是,这似乎不起作用,编译器抱怨说:
However, this doesn't seem to work, as the compiler complains, saying:
struct std.regex.Regex(Char) is used as a type
$ b类型
$ b
那我应该怎么用呢?
So what should I use instead?
推荐答案
如果更改<$,它将编译c $ c> Regex 到 Regex!char
。
原因是 Regex
是可以使用任何字符大小的模板: char
用于UTF-8模式, wchar
(对于UTF-16),或 dchar
(对于UTF-32)。编译器说您需要通过在其中传递必需的 Char
参数来创建类型,以便在此处使用它。
The reason is that Regex
is a template that can use any character size: char
for UTF-8 patterns, wchar
for UTF-16, or dchar
for UTF-32. The compiler is saying you need to create a type by passing the required Char
argument there to use it here.
由于您正在使用由 char
s组成的 string
字符串,因此 Regex!char
Since you are working with string
, which is made up of char
s, Regex!char
is the type to use.
string check_for_match (string input, Regex!char r, string error_message) { return null; }
这篇关于返回std.regex.regex的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!