michelc Blog

Just <strong>another</strong> WordPress.com weblog

Archive for the ‘regex’ Category

Capitalization

leave a comment »

D’après C# Regular Expressions :

using System.Text.RegularExpressions;

public class MyClass {

	public static void Main() {
		string text = "the quick red fox jumped over the lazy brown DOG.";
		System.Console.WriteLine("text=[" + text + "]");
		string result = Regex.Replace(text, @"w+", new MatchEvaluator(MyClass.CapText));
		System.Console.WriteLine("result=[" + result + "]");
		System.Console.ReadLine();
	}

	static string CapText(Match m) {
		string temp = m.ToString();
		temp = char.ToUpper(temp[0]) + temp.Substring(1, temp.Length - 1).ToLower();
		return temp;
	}

}

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/705)

Edit : commentaire de utagger :

Here’s a shorter version:

protected void Button1_Click(object sender, EventArgs e) {
Label1.Text = Regex.Replace(TextBox1.Text, @ »\b\w », new MatchEvaluator(stam));
}

protected string stam(Match m) {
return m.Value.ToUpper();
}

(the trick is using \b which is a 0-length match of word boundaries, including ^ and \s)

Written by michel

14 septembre 2005 at 4:58

Publié dans c#, Code Snippets, regex

Reconnaitre un numéro de sécurité sociale

leave a comment »

Trouvé sur http://u-blog.net/dda/note/11 où tout est expliqué.

([12][0-9][0-9](0[1-9]|1[0-2])(0[1-9]|[13456789][0-9]|2[023456789AB])[0-9][0-9][0-9][0-9][0-9][0-9])([0-9][0-9])

(publié à l’origine sur http://www.bigbold.com/snippets/posts/show/170)

Written by michel

15 avril 2005 at 3:30

Publié dans Code Snippets, regex