人生是一场不能存盘的RPG,我只能尽量多搞几个Screenshot

March 9, 2007

用户名Regex

Filed under: Regular Expression

只接受非空的输入,可由字符,音标,数字,空格,括号等组成, 不能以空格开头.
“^[\p{L}\p{Mn}\p{N}\p{Pc}\p{Po}\p{Ps}\p{Pe}\p{Pd}]+[\p{L}\p{Mn}\p{Zs}\p{N}\p{Pc}\p{Po}\p{Ps}\p{Pe}\p{Pd}]?$”

\p{L} - Allows all letters

\p{Mn} - Allows non-spacing marks including accents and umlauts

\p{Zs} - Allows space separator characters including normal space

\p{N} - Allows all numbers

\p{Ps} - Allows all Open characters such as {, ( and [ but not <

\p{Pe} - Allows all Close characters such as }, ) and ] but not >

\p{Sm} - Allows all math characters

\p{Pc} - represent connection punctuation, such as “_”,

\p{Po} - represent punctuations.

\p{Pd} - Dashes and hyphens.

June 6, 2006

Regex.Replace 中MatchEvaluator 的使用

Filed under: Regular Expression

using System.Text.RegularExpressions;

class RegExSample
{
static string CapText(Match m)
{
// Get the matched string.
string x = m.ToString();
// If the first char is lower case…
if (char.IsLower(x[0]))
{
// Capitalize it.
return char.ToUpper(x[0]) + x.Substring(1, x.Length-1);
}
return x;
}

static void Main()
{
string text = “four score and seven years ago”;
System.Console.WriteLine(”text=[” + text + “]”);
string result = Regex.Replace(text, @”\w+”, new MatchEvaluator(RegExSample.CapText));
System.Console.WriteLine(”result=[” + result + “]”);
}
}
在replace时传入一个delegate ,完全DIY.

正则表达式的替换

Filed under: Regular Expression

想做这样一件事:

把命令行参数中的敏感信息滤掉,比如将

Tool /U:”my Name” /P:”my password” 替换为 Tool /U:”<UserName>” /P:”<Password>”

Private Function HideUserInfo(ByVal para As String) As String
Dim result = para
If (para.IndexOf(”/P:”) > 0) Then
result = Me.ReplaceParameter(result, “/P:”, “<Password>”)
End If
If (para.IndexOf(”/U:”) > 0) Then
result = Me.ReplaceParameter(result, “/U:”, “<UserName>”)
End If
Return result
End Function

Private Function ReplaceParameter(ByVal paras As String, ByVal para As String, ByVal dummy As String) As String
Dim regexStr As String = String.Format(”{0}\”"([^\”"]*)\”"”, para)
‘Dim match As Match = Regex.Match(paras, regexStr)
‘ Console.WriteLine(match.Groups(0))
‘ Console.WriteLine(match.Groups(1))
Dim result As String = Regex.Replace(paras, regexStr, String.Format(”{0}”"{1}”"”, para, dummy))
Return result
End Function

本来是想在ReplaceParameter中替换掉group(1)所对应的内容,但不知道怎么写,只好用了上面的代码.

此外还试了

Regex.Replace(paras, regexStr, String.Format(”$1 “+dummy))

这个写法可以用源字符串中的字符(group1)来生成替换字符串

还可以给group 命名:
Dim regexStr As String = String.Format(”{0}\”"(?<title>[^\”"]*)\”"”, para)

Regex.Replace(paras, regexStr, String.Format(”${title} “+dummy))

大小写要统一.

May 15, 2006

正则表达式使用

Filed under: Regular Expression

—-代码—-

using System.Text.RegularExpressions;

string regexStr = "href\\s*=\\s*\"([^\"]*)\"";
Regex regex = new Regex (regexStr, RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(text);

foreach (Match match in matches)
{
    //do sth…
}

—-正则表达式收集—-

//———————————————————–
//形如 href="http://msdn.microsoft.com/netframework/"的连接
//—-注意Group的使用\"([^\"]*)\"
//—-如果使用Groups[1],会返回  http://msdn.microsoft.com/netframework/
//—-如果使用Groups[0],会返回  href="http://msdn.microsoft.com/netframework/"

string regexStr = "href\\s*=\\s*\"([^\"]*)\""
foreach (Match match in matches)
{
    string myURL = match.Groups[1];
}       






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham