- UID
- 1066743
|
java,C#中运用正则表达式
java
replace:
可以在replace中运用如下:
String oldNum=tempLandNum.replaceAll("\\D+", ""); //只取字符串中的数字
匹配提取值:
如下:匹配价格中的数值
private final static Pattern PATTERN_PRICE = Pattern
.compile("(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))");
Matcher matcher = PATTERN_PRICE.matcher(propertyValue);
if (matcher.find()) {
float value = Float.valueOf(matcher.group()) * unitTime;
area.setValue(value);
area.setUseValue(String.valueOf(value));
} else {
area.setUseValue(propertyValue);
}
split:
String ipstring="59.64.159.224";
String iparray[]=ipstring.split("\\.");
for(String stemp:iparray){
System.out.println(stemp);
}
C#
Split
Split方法是把由正则表达式匹配项定义的位置将输入字符串拆分为一个子字符串数组。例如:
Regex r = new Regex("-"); // Split on hyphens.
string[] s = r.Split("first-second-third");
for(int i=0;i<s.Length;i++)
{
Response.Write(s+"<br>");
}
运行的结果是:
First
Second
Third
匹配提取值:
string patternDate = @"^\d{4}[/]([0][1-9]|(1[0-2]))[/]([1-9]|([012]\d)|(3[01]))([ \t\n\x0B\f\r])(([0-1]{1}[0-9]{1})|([2]{1}[0-4]{1}))([:])(([0-5]{1}[0-9]{1}|[6]{1}[0]{1}))([:])((([0-5]{1}[0-9]{1}|[6]{1}[0]{1})))$";//匹配格式如2012/12/15 18:00:00
Match m = Regex.Match("2014/11/02 06:12:21AADASSA", patternDate);
if (m.Success){
Console.WriteLine("Match="+m.ToString());
}
replace:
string s = Regex.Replace("abracadabra", "abra", "zzzz");
js中
替换所有html标签
name.replace(/<[^>]+>/g,"")
Jsp中
注意EL的replace不支持正则
${fn:replace(topArticle.name,'<br></a>','')} |
|