English 中文(简体)
Dpind.regex - 作为案文的返回
原标题:D std.regex - return as text
  • 时间:2012-05-01 07:19:52
  •  标签:
  • regex
  • d

Is there a possibility to return text which was used to create regular expression?

与此类似:

auto r = regex(r"[0-9]", "g"); // create regular expression
writeln(r.dumpAsText());       // this would write: [0-9]

http://dlang.org/phobos/std_regex.html 页: 1 (或至少我没有通知)

最佳回答

无,因为它汇编了该公报,我不认为它甚至在汇编之后就储存了该公报。

最好的事就是在制造时储存铺.。

资料来源:struct Regex

As you can see, it doesn t store the pattern string, only the bytecode.

问题回答

通常使用一种亚类会起作用,但不幸的是,由于模板的局限性, does然不灵。 例如,一种可行的解决办法(现在就没有工作),就是将reg作为子类:

auto myregex(string arg1, string arg2)
{
    struct RegexWrap
    {
        Regex!char reg;
        alias reg this;
        string dumpAsText;
    }

    return RegexWrap(regex(arg1, arg2), arg1);
}

void main()
{
    auto r = myregex(r"[0-9]", "g"); // create regular expression
    writeln(r.dumpAsText);       // this would write: [0-9]   
    writeln(match("12345", r));  // won t work 
}

The match function in setd. 即使在使用一种子类时,reg也获得了这种包装的 t子,因为它没有这一模板限制:

public auto match(R, RegEx)(R input, RegEx re)
    is(RegEx == Regex!(BasicElementOf!R)

即便你改变了主人,它仍然赢得以下工作:

public auto match(R)(R input, Regex!(BasicElementOf!R) re)

唯一可行的办法是,如果这种类型明确无误,就可以通过该次类型:

public auto match(R)(R input, Regex!char re)

我认为,这是可以改进的D的一个薄弱环节。





相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签