C#中的正则表达式的Guid [英] C# Regex for Guid
问题描述
我需要通过一个字符串来解析和周围的每个GUID值加单引号。我想我可以使用正则表达式来做到这一点,但我不完全是一个正则表达式大师。
是否有一个良好的正则表达式用来标识一个GUID?
我的第二个问题是,一旦我找到了一个有效的正则表达式我假设我会使用 Regex.Replace(字符串,字符串,MatchEvaluator)
,但我不十分肯定的语法。也许是这样的:
返回Regex.Replace(stringToFindMatch,GuidRegex,比赛= GT;
{
返回的String.Format('{0}',match.Groups [0]的ToString());
});
这是我试图解析字符串可能看起来像这样:
SELECT
passwordco0_.PASSWORD_CONFIG_ID为PASSWORD1_46_0_,
从
PASSWORD_CONFIG passwordco0_
哪里
passwordco0_.PASSWORD_CONFIG_ID = baf04077-a3c0-454b-ac6f-9fec00b8e170;
@ P0 = baf04077-a3c0-454b-ac6f-9fec00b8e170 [类型:GUID(0)]
块引用>解决方案这一个是很简单,就像你说的,不需要委托。
resultString = Regex.Replace(subjectString,
?@^ [{(] [0-9A-F] {8} [ - ]([0-9A-F] {4}?[ - ]?){3} [0-9A-F] {12 } [)}] $,
'$ 0',
RegexOptions.IgnoreCase);这符合以下样式,这是一个GUID。
都是等价的和可接受的格式ca761232ed4211cebacd00aa0057b223
CA761232-ED42-11CE-BACD-00AA0057B223
{CA761232-ED42-11CE-BACD-00AA0057B223}
(CA761232-ED42-11CE-BACD-00AA0057B223)I need to parse through a string and add single quotes around each Guid value. I was thinking I could use a Regex to do this but I'm not exactly a Regex guru.
Is there a good Regex to use to identify a Guid?
My second question is once I've found a valid regex I'm assuming I would use
Regex.Replace(String, String, MatchEvaluator)
but I'm not quite sure of the syntax. Maybe something like:return Regex.Replace(stringToFindMatch, GuidRegex, match => { return string.Format("'{0}'", match.Groups[0].ToString()); });
A string that I'm trying to parse may look like this:
"SELECT passwordco0_.PASSWORD_CONFIG_ID as PASSWORD1_46_0_, FROM PASSWORD_CONFIG passwordco0_ WHERE passwordco0_.PASSWORD_CONFIG_ID=baf04077-a3c0-454b-ac6f-9fec00b8e170; @p0 = baf04077-a3c0-454b-ac6f-9fec00b8e170 [Type: Guid (0)]"
解决方案This one is quite simple and does not require a delegate as you say.
resultString = Regex.Replace(subjectString, @"^[{(]?[0-9A-F]{8}[-]?([0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$", "'$0'", RegexOptions.IgnoreCase);
This matches the following styles, which are all equivalent and acceptable formats for a GUID.
"ca761232ed4211cebacd00aa0057b223" "CA761232-ED42-11CE-BACD-00AA0057B223" "{CA761232-ED42-11CE-BACD-00AA0057B223}" "(CA761232-ED42-11CE-BACD-00AA0057B223)"
这篇关于C#中的正则表达式的Guid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!