using System;
using System.Text.RegularExpressions;
class Program
{
static void Main(string[] args)
{
string str = "<You're angle & evil>";
string pattern = "'|&|<|>";
Regex regex = new Regex(pattern);
Program prog = new Program();
MatchEvaluator evaluator = new MatchEvaluator(prog.ConvertToXML);
Console.WriteLine(regex.Replace(str, evaluator));
Console.Read();
}
//把正則表達式的匹配到的字符轉換成xml能正常識別的標識
public string ConvertToXML(Match m)
{
//string s0=m.Groups[0].Value;
//string s1=m.Groups[1].Value;
//string s2=m.Groups[2].Value;
switch (m.Value)
{
case "'":
return "&apos";
case "&":
return "&";
case "<":
return "<";
case ">":
return ">";
default:
return "";
}
}
}
string str = "<You're angle & evil>";
string pattern = "['&<>]";
//str = Regex.Replace(str, pattern, new MatchEvaluator(delegate(Match m) { // or
str = Regex.Replace(str, pattern, m =>
{
switch (m.Value)
{
case "'":
return "&apos";
case "&":
return "&";
case "<":
return "<";
case ">":
return ">";
default:
return "";
}
});
struct UInt96
{
ulong hi;
uint lo;
// 構造函數
public UInt96(ulong h, uint l)
{
hi = h;
lo = l;
}
// 返回加1後的結果
public UInt96 Inc()
{
if (lo < 0xFFFFFFFF) return new UInt96(hi, lo + 1);
return new UInt96(hi + 1, 0);
}
// 自增運算符
public static UInt96 operator ++(UInt96 x)
{
return x.Inc();
}
// 十六進制字符串表示
public override string ToString()
{
return string.Join(" ", Regex.Split(string.Format("{0:X16}{1:X8}", hi, lo), "(?!^)(?=(?:.{4})+$)"));
}
}
string s = "1234567890";
string t = string.Join(" ", Regex.Split(s, "(?!^)(?=(?:.{4})+$)"));
// 就是把一個字符串從右往左每4位加一個空格,結果是:"12 3456 7890"
string s = "1234567890";
// 考慮了一下,這樣更簡單:
string t = Regex.Replace(s, "(?!^)(?=(?:.{4})+$)", " ");
// 把一個字符串從右往左每4位加一個空格,結果是:"12 3456 7890"
// 去掉SQL語句中的註釋
static string RemoveSqlComment(string sql)
{
return Regex.Replace
(
sql,
@"(?ms)'[^']*'|--.*?$|//*.*?/*/",
delegate(Match m)
{
switch (m.Value[0])
{
case '-': return "";
case '/': return " ";
default : return m.Value;
}
}
);
}
string s = "某某的面積是12345678平方米";
string t = Regex.Replace(s, @"(?<=/d)(?=(/d/d/d)+(?!/d))", ",");
// 同理,這個正則的作用是把一串數字每3位加一個逗號。
Console.WriteLine(t); // 輸出:某某的面積是12,345,678平方米
using System;
using System.Text.RegularExpressions;
class Program
{
// 把阿拉伯數字的金額轉換爲中文大寫數字
static string ConvertToChinese(double x)
{
string s = x.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L/.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[/.]|$))))", "${b}${z}");
return Regex.Replace(d, ".", delegate(Match m) { return "負元空零壹貳叄肆伍陸柒捌玖空空空空空空空分角拾佰仟萬億兆京垓秭穰"[m.Value[0] - '-'].ToString(); });
}
static void Main()
{
Random r = new Random();
for (int i = 0; i < 10; i++)
{
double x = r.Next() / 100.0;
Console.WriteLine("{0,14:N2}: {1}", x, ConvertToChinese(x));
}
}
}
/* 可能的輸出:
5,607,400.68: 伍佰陸拾萬柒仟肆佰元陸角捌分
2,017,723.33: 貳佰零壹萬柒仟柒佰貳拾叄元叄角叄分
751,181.17: 柒拾伍萬壹仟壹佰捌拾壹元壹角柒分
7,849,851.53: 柒佰捌拾肆萬玖仟捌佰伍拾壹元伍角叄分
2,629,143.90: 貳佰陸拾貳萬玖仟壹佰肆拾叄元玖角
13,461,629.68: 壹仟叄佰肆拾陸萬壹仟陸佰貳拾玖元陸角捌分
4,594,391.16: 肆佰伍拾玖萬肆仟叄佰玖拾壹元壹角陸分
13,046,560.60: 壹仟叄佰零肆萬陸仟伍佰陸拾元陸角
13,041,371.21: 壹仟叄佰零肆萬壹仟叄佰柒拾壹元貳角壹分
20,639,609.44: 貳仟零陸拾叄萬玖仟陸佰零玖元肆角肆分
*/