English 中文(简体)
备注 - 重新使用低压
原标题:RegEx - reusing subexpressions
  • 时间:2011-11-20 19:51:35
  •  标签:
  • c#
  • .net
  • regex

Saye 我有一套六十二条轨道编号:

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

当我建造一个小区时,我需要多时间,例如这样。

(?<from>[0-9a-fA-F]{1,8})s*:s*(?<to>[0-9a-fA-F]{1,8})

Do I have to repeat the subexpression definition every time, or is there a way to "name and reuse" it?

我想象像这样的东西(预警,发明的辛塔克斯!)

(?<from>{hexnum=[0-9a-fA-F]{1,8}})s*:s*(?<to>{=hexnum})

<代码>hexnum=将界定“hexnum”,{=hexnum}将重新使用。

Since I already learnt it matters: I m using .NET s System.Text.RegularExpressions.Regex, but a general answer would be interesting, too.

问题回答

RegEx Subroutines

当你想多次使用次抑郁症而不重写时,你可以将其称为<>次>。 可根据姓名、指数或相对位置指定替代企业。

辅助设施由个人防护中心、Perl、Ruby、PHP、Delphi、R等机构提供支助。 不幸的是,网络框架缺乏,但有一些免费图书馆。 https://github.com/ltrzesniewski/pcre-net rel=“noretinger”https://github.com/ltrzesniewski/pcre-net

Syntax

这里指的是非常规工作:请说的是,你希望连续三次重复使用<条码>。

Standard RegEx
Any: [abc][abc][abc]

Subroutine, by Name
Perl:     (? name [abc])(?&name)(?&name)
PCRE: (?P<name>[abc])(?P>name)(?P>name)
Ruby:   (?<name>[abc])g<name>g<name>

Subroutine, by Index
Perl/PCRE: ([abc])(?1)(?1)
Ruby:          ([abc])g<1>g<1>

Subroutine, by Relative Position
Perl:     ([abc])(?-1)(?-1)
PCRE: ([abc])(?-1)(?-1)
Ruby:   ([abc])g<-1>g<-1>

Subroutine, Predefined
This defines a subroutine without executing it.
Perl/PCRE: (?(DEFINE)(? name [abc]))(?P>name)(?P>name)(?P>name)

Examples

Matches a valid IPv4 address string, from 0.0.0.0 to 255.255.255.255:
((?:25[0-5])|(?:2[0-4][0-9])|(?:[0-1]?[0-9]?[0-9])).(?1).(?1).(?1)

Without subroutines:
((?:25[0-5])|(?:2[0-4][0-9])|(?:[0-1]?[0-9]?[0-9])).((?:25[0-5])|(?:2[0-4][0-9])|(?:[0-1]?[0-9]?[0-9])).((?:25[0-5])|(?:2[0-4][0-9])|(?:[0-1]?[0-9]?[0-9])).((?:25[0-5])|(?:2[0-4][0-9])|(?:[0-1]?[0-9]?[0-9]))

And to solve the original posted problem:
(?<from>(?P<hexnum>[0-9a-fA-F]{1,8}))s*:s*(?<to>(?P>hexnum))

More Info

http://regular-expressions.info/subroutine.html
http://regex101.com/

为什么不这样做,不是真正缩短,而是更可以维持。

String.Format("(?<from>{0})s*:s*(?<to>{0})", "[0-9a-zA-Z]{1,8}");

If you want more self documenting code i would assign the number regex string to a properly named const variable.

。 NET regex型式不支持再入侵模式,如果你能够使用:(?< From><hex>[0-9a-fA-F]{1,8})s*:s*(?<to>hex>hex>)后可在Rub和PHP/PC中单独列出。

从C#6开始,你可以使用一种被污染的插手法,它看起来像PCRE/Onigmo子爱国者再行一样,但实际上比较清洁,如果该团体的名称与“技术”捕获组相同,则不会出现任何潜在的瓶颈:

http://ideone.com/VOVlkV”rel=“nofollow noreferer”> C# demo:

using System;
using System.Text.RegularExpressions;

public class Test
{
    public static void Main()
    {
        var block = "[0-9a-fA-F]{1,8}";
        var pattern = $@"(?<from>{block})s*:s*(?<to>{block})";
        Console.WriteLine(Regex.IsMatch("12345678  :87654321", pattern));
    }
}

<代码>$@>. > > <>>>>>> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > ......> > > > > > > > > > > > > > > > > > > > > > > > > ......> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > (e) <代码>$@”(?:{})

旧C#版本,使用<代码>。 格式/代码:

var pattern = string.Format(@"(?<from>{0})s*:s*(?<to>{0})", block);

https://stackoverflow.com/a/8204328/3832970”

如果我正确理解你的问题,你想要重新利用某些模式来构建一个更大的模式?

string f = @"fcd+/";
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.

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

www.un.org/Depts/DGACM/index_french.htm

这份样本案文不是标题,因为它没有2个空间。

没有这种预先界定的类别。 我认为,你可以采用忽视的办法加以简化,例如:

(?i)(?<from>[0-9a-z]{1,8})s*:s*(?<to>[0-9a-z]{1,8})

重新使用被点名的捕获组使用这一辛加税:k<name>或 k name

因此,答案是:

(?<from>[0-9a-fA-F]{1,8})s*:s*k<from>

More info:





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...