简体   繁体   English

在C#中使用正则表达式提取字符串

[英]Extract string with regex in C#

I have this string in C#: "/19/Ora01" and i want to exctract "Ora01" 我在C#中有此字符串:“ / 19 / Ora01”,我想吸引“ Ora01”

I have tried with no success: 我尝试没有成功:

var pattern = @"^(Ora[0-9]{2})$";

var match = Regex.Match(args.Path, pattern);

var hour = match.Groups[0].Value;

What I'm doing wrong? 我做错了什么?

What about to exctract two other strings, one with 19, the other with Ora01? 怎样吸引另外两个字符串,一个带有19,另一个带有Ora01?

如果您将/ Split() / Split() /如果字符串为/19/Ora01模式,该/19/Ora01

var data = "/19/Ora01".Split("/", StringSplitOptions.RemoveEmptyEntries);

This is what you want with Regex: 这是正则表达式要的:

public static void Main(string[] args)
  {
    string pattern = "\\/(.*?)\\/(.+)";
    string text = "/19/Ora01";
    Match match = Regex.Match(text, pattern);
    string first = match.Groups[1].Value;
    string second = match.Groups[2].Value;

    Console.WriteLine(first);
    Console.WriteLine(second);

    Console.ReadLine();
  }

Consider also the answer provided by Rahul . 还考虑拉胡尔提供的答案。

^ means "start of string" and "$" means "end of string. You asked for something that contains only Ora followed by two digits. Remove ^ at least. You don't need grouping either. Ora[0-9]{2} should work : ^表示“字符串的开头”,“ $”表示“字符串的结尾。”您要求的内容仅包含Ora后跟两位数字。至少删除^。您也不需要分组Ora[0-9]{2}应该工作:

var pattern = @"Ora[0-9]{2}";

var match = Regex.Match("/19/Ora01", pattern);

var hour = match.Groups[0].Value;
hour.Dump();

Prints Ora01 . 打印Ora01

If you want the hour part after Ora you'd have to group the digits : 如果您想要在Ora之后的小时部分,则必须将数字分组:

var pattern = @"Ora(?<hour>[0-9]{2})";

var match = Regex.Match("/19/Ora01", pattern);

var hour = match.Groups["hour"].Value;
Console.WriteLine(hour);

This prints : 打印:

01

You don't explain what kind of validation you want to perform, eg whether the slashes matter or not, whether 19 should be there and eg be treated as a year. 您无需说明要执行哪种验证,例如,斜杠是否重要,是否应存在19斜杠,例如将其视为一年。

If you want to ensure the string follows this format and the first part is two-digit number, you could use eg @"/(?<year>\\d{2})/Ora(?<hour>\\d{2})" : 如果要确保字符串遵循此格式并且第一部分是两位数字,则可以使用例如@"/(?<year>\\d{2})/Ora(?<hour>\\d{2})"

var pattern = @"/(?<year>\d{2})/Ora(?<hour>\d{2})";

var match = Regex.Match("/19/Ora01", pattern);

var hour = match.Groups["hour"].Value;
var year = match.Groups["year"].Value;
Console.WriteLine($"{year} {hour}");

This prints : 打印:

19 01

Only if the format is correct. 仅当格式正确时。

In regular expression ^ means match from start of the string. 在正则表达式中, ^表示从字符串开头开始匹配。

Just like $ means match to end of the string. 就像$表示匹配到字符串的结尾。

Simply remove ^ from your pattern and your code should work. 只需从您的模式中删除^ ,您的代码就可以工作。

Regex is the best way to do this: 正则表达式是执行此操作的最佳方法:

string pattern = "\\/(.*?)\\/(.+)";
string text = "/19/Ora01";
Match match = Regex.Match(text, pattern);
string first = match.Groups[1].Value;
string second = match.Groups[2].Value;

Console.WriteLine(first);
Console.WriteLine(second);

Also @Panagiotis Kanavos is probably the best answer and the most complete one. 另外,@ Panagiotis Kanavos可能是最好的答案,也是最完整的答案。

You could use string.Split for this, too. 您也可以使用string.Split。 Might be faster then regex but depends on your needs. 可能比regex更快,但要取决于您的需求。

With string.Split this could look like 使用string.Split可能看起来像

var result = start.Substring(start.LastIndexOf("/") + 1);

If you want to use regex you have to remove the "^". 如果要使用正则表达式,则必须删除“ ^”。 For easy testing and more information have a look at regexr.com 为便于测试和更多信息,请访问regexr.com。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM