C# 正则表达式在模式中提取分隔文本
我试图解析一种带有引号或尖括号中的关键字的标记C# 正则表达式在模式中提取分隔文本,c#,regex,C#,Regex,我试图解析一种带有引号或尖括号中的关键字的标记 “之间的单词是静态关键字,而之间的单词是动态关键字 样本: * Say "hello" to "world" * Say <something> to <somebody> * I can also be a plain statement 更新2 有一些人建议分步骤执行此操作,即在第一步中获取所有有效行,然后在每一行中查找关键字。我希望将此作为我的最后一个选项,上下文是此代码的使用者需要知道关键字及其在整个字符串中的位置
“
之间的单词是静态关键字,而
之间的单词是动态关键字
样本:
* Say "hello" to "world"
* Say <something> to <somebody>
* I can also be a plain statement
更新2
有一些人建议分步骤执行此操作,即在第一步中获取所有有效行,然后在每一行中查找关键字。我希望将此作为我的最后一个选项,上下文是此代码的使用者需要知道关键字及其在整个字符串中的位置。因此,维护偏移量是我在spl中需要的开销单击父字符串。下面的表达式将提取所有关键字。请尝试
/// <summary>
/// A description of the regular expression:
///
/// Beginning of line or string
/// [1]: A numbered capture group. [.*?\"(?<keyword>.*?)\"], one or more repetitions
/// .*?\"(?<keyword>.*?)\"
/// Any character, any number of repetitions, as few as possible
/// Literal "
/// [keyword]: A named capture group. [.*?]
/// Any character, any number of repetitions, as few as possible
/// Literal "
///
///
/// </summary>
public static Regex regex = new Regex(
"^(.*?\\\"(?<keyword>.*?)\\\")+",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
);
// Capture all Matches in the InputText
MatchCollection ms = regex.Matches(InputText);
//
///正则表达式的说明:
///
///行首
///[1]:编号的捕获组。[.*?\”(?.*?\”],一个或多个重复
/// .*?\"(?.*?)\"
///任何字符,任何重复次数,尽可能少
///“文字”
///[关键字]:命名的捕获组。[.*?]
///任何字符,任何重复次数,尽可能少
///“文字”
///
///
///
公共静态正则表达式Regex=新正则表达式(
"^(.*?\\\"(?.*?)\\\")+",
RegexOptions.IgnoreCase
|RegexOptions.Multiline
);
//捕获InputText中的所有匹配项
MatchCollection ms=regex.Matches(InputText);
使用该工具学习并创建正则表达式,它将有助于创建C#或VB.NET代码下面的表达式将提取所有关键字。试试吧
/// <summary>
/// A description of the regular expression:
///
/// Beginning of line or string
/// [1]: A numbered capture group. [.*?\"(?<keyword>.*?)\"], one or more repetitions
/// .*?\"(?<keyword>.*?)\"
/// Any character, any number of repetitions, as few as possible
/// Literal "
/// [keyword]: A named capture group. [.*?]
/// Any character, any number of repetitions, as few as possible
/// Literal "
///
///
/// </summary>
public static Regex regex = new Regex(
"^(.*?\\\"(?<keyword>.*?)\\\")+",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
);
// Capture all Matches in the InputText
MatchCollection ms = regex.Matches(InputText);
public static Regex regex = new Regex(
"^\\*.*(<|\")(\\w+)(>|\")",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
//
///正则表达式的说明:
///
///行首
///[1]:编号的捕获组。[.*?\”(?.*?\”],一个或多个重复
/// .*?\"(?.*?)\"
///任何字符,任何重复次数,尽可能少
///“文字”
///[关键字]:命名的捕获组。[.*?]
///任何字符,任何重复次数,尽可能少
///“文字”
///
///
///
公共静态正则表达式Regex=新正则表达式(
"^(.*?\\\"(?.*?)\\\")+",
RegexOptions.IgnoreCase
|RegexOptions.Multiline
);
//捕获InputText中的所有匹配项
MatchCollection ms=regex.Matches(InputText);
使用该工具学习并创建正则表达式,它将有助于创建C#或VB.NET代码下面的表达式将提取所有关键字。试试吧
/// <summary>
/// A description of the regular expression:
///
/// Beginning of line or string
/// [1]: A numbered capture group. [.*?\"(?<keyword>.*?)\"], one or more repetitions
/// .*?\"(?<keyword>.*?)\"
/// Any character, any number of repetitions, as few as possible
/// Literal "
/// [keyword]: A named capture group. [.*?]
/// Any character, any number of repetitions, as few as possible
/// Literal "
///
///
/// </summary>
public static Regex regex = new Regex(
"^(.*?\\\"(?<keyword>.*?)\\\")+",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
);
// Capture all Matches in the InputText
MatchCollection ms = regex.Matches(InputText);
public static Regex regex = new Regex(
"^\\*.*(<|\")(\\w+)(>|\")",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
//
///正则表达式的说明:
///
///行首
///[1]:编号的捕获组。[.*?\”(?.*?\”],一个或多个重复
/// .*?\"(?.*?)\"
///任何字符,任何重复次数,尽可能少
///“文字”
///[关键字]:命名的捕获组。[.*?]
///任何字符,任何重复次数,尽可能少
///“文字”
///
///
///
公共静态正则表达式Regex=新正则表达式(
"^(.*?\\\"(?.*?)\\\")+",
RegexOptions.IgnoreCase
|RegexOptions.Multiline
);
//捕获InputText中的所有匹配项
MatchCollection ms=regex.Matches(InputText);
使用该工具学习并创建正则表达式,它将有助于创建C#或VB.NET代码下面的表达式将提取所有关键字。试试吧
/// <summary>
/// A description of the regular expression:
///
/// Beginning of line or string
/// [1]: A numbered capture group. [.*?\"(?<keyword>.*?)\"], one or more repetitions
/// .*?\"(?<keyword>.*?)\"
/// Any character, any number of repetitions, as few as possible
/// Literal "
/// [keyword]: A named capture group. [.*?]
/// Any character, any number of repetitions, as few as possible
/// Literal "
///
///
/// </summary>
public static Regex regex = new Regex(
"^(.*?\\\"(?<keyword>.*?)\\\")+",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
);
// Capture all Matches in the InputText
MatchCollection ms = regex.Matches(InputText);
public static Regex regex = new Regex(
"^\\*.*(<|\")(\\w+)(>|\")",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
//
///正则表达式的说明:
///
///行首
///[1]:编号的捕获组。[.*?\”(?.*?\”],一个或多个重复
/// .*?\"(?.*?)\"
///任何字符,任何重复次数,尽可能少
///“文字”
///[关键字]:命名的捕获组。[.*?]
///任何字符,任何重复次数,尽可能少
///“文字”
///
///
///
公共静态正则表达式Regex=新正则表达式(
"^(.*?\\\"(?.*?)\\\")+",
RegexOptions.IgnoreCase
|RegexOptions.Multiline
);
//捕获InputText中的所有匹配项
MatchCollection ms=regex.Matches(InputText);
使用该工具学习并创建正则表达式,它将有助于创建C#或VB.NET代码(
public static Regex regex = new Regex(
"^\\*.*(<|\")(\\w+)(>|\")",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
“^\\*.*(publicstaticregex Regex=newregex(
“^\\*.*(publicstaticregex Regex=newregex(
“^\\*.*(publicstaticregex Regex=newregex(
"^\\*.*(
您可以分两步完成此操作。首先抓取从*
开始的行。请参阅演示
然后,您可以通过捕获或匹配来获取关键字
见演示
您可以分两步完成此操作。首先抓取从*
开始的行。请参阅演示
然后,您可以通过捕获或匹配来获取关键字
见演示
您可以分两步完成此操作。首先抓取从*
开始的行。请参阅演示
然后,您可以通过捕获或匹配来获取关键字
见演示
您可以分两步完成此操作。首先抓取从*
开始的行。请参阅演示
然后,您可以通过捕获或匹配来获取关键字
见演示
看看如何通过正则表达式将行提取到字符串中,然后将其拆分?string[]result=regexString.split(“”);
。您的结果将是每个偶数索引(2,4,6,8..)。@C4ud3x-Ah但有一个问题-如果拆分行,我需要跟踪偏移量(即行号)对于这个比特的消费者,我正试图避免它。是的,这可能是一个不好的方法。检查Coder Hawk的解决方案。它只抓取关键字。正则表达式I