Return value of std.regex.regex?

这一生的挚爱 提交于 2019-12-23 21:45:53

问题


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 chars, 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!