using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions;namespace ConsoleApplication14 { class Program { static void Main(string[] args) { string str = "werfsfsd1234567dfg"; str= Regex.Replace(str, @"[a-z]", ""); Console.WriteLine(str); Console.ReadKey(); } } }
Regex objRegex = new Regex(@"(?<=\D|^)(?:(?:9(?=8|\D))?(?:8(?=7|\D))?(?:7(?=6|\D))?(?:6(?=5|\D))?(?:5(?=4|\D))?(?:4(?=3|\D))?(?:3(?=2|\D))?(?:2(?=1|\D))?(?:1(?=0|\D))?(?:0(?=\D|$))?)(?=\D|$)"); MatchCollection objMatches = objRegex.Matches(str);
提取的话,这样就行了 Regex reg = new Regex(@"\d{7}"); MatchCollection mc = reg.Matches(yourStr); foreach (Match m in mc) { richTextBox2.Text += m.Value + "\n"; }
^[\w\W]*\d{7}[\w\W]*$
^[\d\D]*\d{7}[\d\D]*$
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;namespace ConsoleApplication14
{
class Program
{
static void Main(string[] args)
{
string str = "werfsfsd1234567dfg";
str= Regex.Replace(str, @"[a-z]", "");
Console.WriteLine(str);
Console.ReadKey();
}
}
}
MatchCollection objMatches = objRegex.Matches(str);
MatchCollection mc = reg.Matches(yourStr);
foreach (Match m in mc)
{
richTextBox2.Text += m.Value + "\n";
}