正则表达式在C#中的用法: 三种情况举例 A B Our W a y to home our way to home O u r way to home our way to home our way to h o m e our way to home要用正确的B来代替A,如何用正则表达式来表示A的三种情况和B?? (A1:句中有2个以上字母以空格间隔出现) (A2:句首有2个以上字母以空格间隔出现) (A3:句尾有2个以上字母以空格间隔出现) 急啊 !!
1、不知道想干什么,标题说repace(replace?),正文没看到和这有什么关系 2、没看出哪个是B 3、O u r,这不是正则能解决的问题,除非给出明确的规则
A B Our W a y to home our way to home O u r way to home our way to home our way to h o m e our way to home我确定以及肯定这是可以用正则解决的问题,而且已经cover到A1部分情况了,就是不知道如何全部cover,实现如下 (\s+[a-zA-Z]) ([a-zA-Z]\s+) $1$2 这是利用正则表达式的模式匹配,我有点不熟悉
A Our W a y to home O u r way to home our way to h o m e B our way to home our way to home our way to home
你的意思是一旦出现 I a m here 这样的,就直接合并成 Iam here 就行了,别的不用管?如果是这样,那很简单 Regex reg = new Regex(@"(?<=(?:^|\s)[a-zA-Z])\s(?=[a-zA-Z](?:\s|$))"); string result = reg.Replace(yourStr, ""); richTextBox2.Text = result;
2、没看出哪个是B
3、O u r,这不是正则能解决的问题,除非给出明确的规则
Our W a y to home our way to home
O u r way to home our way to home
our way to h o m e our way to home我确定以及肯定这是可以用正则解决的问题,而且已经cover到A1部分情况了,就是不知道如何全部cover,实现如下
(\s+[a-zA-Z]) ([a-zA-Z]\s+) $1$2
这是利用正则表达式的模式匹配,我有点不熟悉
Our W a y to home
O u r way to home
our way to h o m e B
our way to home
our way to home
our way to home
I a m here
这样的,就直接合并成
Iam here
就行了,别的不用管?如果是这样,那很简单
Regex reg = new Regex(@"(?<=(?:^|\s)[a-zA-Z])\s(?=[a-zA-Z](?:\s|$))");
string result = reg.Replace(yourStr, "");
richTextBox2.Text = result;