用正则表达式 替换 分割字符串
2008-11-11 23:42
330 查看
普通方之一法提取 123{abc}465{xyz}789 中的{}中数据
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str1 = "123{abc}465{xyz}789";
string str2 = "";
foreach (char ch in str1)
{
if (ch >= 97 && ch <= 123)
{
str2 = str2 + ch;
}
}
string[] s = str2.Split('{');
str2 = string.Empty;
for (int i = 1; i < s.Length; i++)//s[0]=null
{
str2 += s[i] + "/r/n";
}
Console.WriteLine(str2);
}
}
}
正则提取
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Regex reg = new Regex("(?<={)[^}]*(?=})");//或者用(?<={).*?(?=})
string str = "123{abc}465{xyz}789";
string result = string.Empty;
foreach (Match m in reg.Matches(str))
{
result += m.Value + "/r/n";
}
Console.Write(result);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str1 = "123{abc}465{xyz}789";
string str2 = "";
foreach (char ch in str1)
{
if (ch >= 97 && ch <= 123)
{
str2 = str2 + ch;
}
}
string[] s = str2.Split('{');
str2 = string.Empty;
for (int i = 1; i < s.Length; i++)//s[0]=null
{
str2 += s[i] + "/r/n";
}
Console.WriteLine(str2);
}
}
}
正则提取
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Regex reg = new Regex("(?<={)[^}]*(?=})");//或者用(?<={).*?(?=})
string str = "123{abc}465{xyz}789";
string result = string.Empty;
foreach (Match m in reg.Matches(str))
{
result += m.Value + "/r/n";
}
Console.Write(result);
}
}
}
相关文章推荐
- Java_正则表达式_按给定正则表达式分割或替换字符串
- Python 正则表达式 字符串的匹配、替换、分割、查找
- python 正则表达式 字符串的 匹配 替换 分割 查找
- EditPlus 正则表达式替换字符串详解
- 还为WORD替换特殊字符串烦恼吗?请使用word通配符其实和正则表达式很类似,学会使用起到事半功倍的效果!
- 正则表达式几种常用功能——查询,提取,替换,分割
- C#中正则表达式进行忽略大小写的字符串替换
- Java正则表达式,匹配、替换字符串中的占位符
- C# 用正则表达式替换字符串中所有特殊字符
- asp常用的正则表达式实现字符串的替换
- java中用正则表达式获取一个指定的子字符在字符串中出现的指定次数的下标索引,并且进行字符串分割
- C# 用正则表达式替换字符串中所有特殊字符
- asp (vbscript)中用正则表达式替换字符串
- Html表单的正则校验--将不符合指定正则表达式的字符串自动替换为空
- 利用正则表达式,对String对象进行替换与分割
- JS字符串替换函数全部替换方法 JS 正则表达式如何使用变量
- C#正则表达式匹配与替换字符串功能示例
- Emacs正则表达式替换时替换字符串中含有回车符无法替换的问题
- EditPlus正则表达式替换字符串详解
- 正则表达式:字符串中连续重复字符串的识别和替换