RegEx - reusing subexpressions

后端 未结 6 1546
傲寒
傲寒 2020-11-28 12:56

Say I have a regex matching a hexadecimal 32 bit number:

([0-9a-fA-F]{1,8})

When I construct a regex where I need to match this multiple ti

6条回答
  •  生来不讨喜
    2020-11-28 13:30

    If I am understanding your question correctly, you want to reuse certain patterns to construct a bigger pattern?

    string f = @"fc\d+/";
    string e = @"\d+";
    Regex regexObj = new Regex(f+e);
    

    Other than this, using backreferences will only help if you are trying to match the exact same string that you have previously matched somewhere in your regex.

    e.g.

    /\b([a-z])\w+\1\b/
    

    Will only match : text, spaces in the above text :

    This is a sample text which is not the title since it does not end with 2 spaces.

提交回复
热议问题