2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C#Advent Calendar 2021

Day 21

C#でも名前付きキャプチャを使う Named Group

Posted at

背景

すっかり忘れてて、C# で使おうとしたら、意外に参照方法に手間取ったための備忘録

概要

基本はこの記事へどうぞ。

実際の使用例

LinqPad での利用例

Match

image.png

Match
	displayName.Dump("<<< displayName >>>");
	var pattern = @"(?<FamilyName>\w+)\.(?<GivenName>\w+)@(?<DomainName>.+)";
	var Match = Regex.Match(displayName, pattern);
	
	if (Match.Success){
		Match.Groups["FamilyName"].Value.Dump("FamilyName");
		Match.Groups["GivenName"].Value.Dump("GivenName");
		Match.Groups["DomainName"].Value.Dump("DomainName");
	}

Matches

image.png

Matches
	var source = "<code>// hage hage </code>\n<code>public string hagehage {get; set;}</code>";
	var pattern = @"<(?<CodeTag>code)>(?<Source>.*)</\k<CodeTag>>";
	var Matches = Regex.Matches(source, pattern);

	foreach (Match match in Matches){
		match.Value.Dump("<<< Value >>>");
		if (match.Success){
			match.Groups["CodeTag"].Value.Dump("CodeTag");
			match.Groups["Source"].Value.Dump("Source");
		}
		else
		{
			"Not Found!".Dump();
		}
	}

keyword

how to use named group of regular expression

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?