问题
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
So what should I use instead?
回答1:
It'll compile if you change Regex
to Regex!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.
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; }
来源:https://stackoverflow.com/questions/24967276/return-value-of-std-regex-regex