This topic has been archived. It cannot be replied.
-
工作学习 / 学科技术讨论 / 一个regular expression问题:如何把连在一起的两个单词分开一份文档里面所有的"abc"单词后面都少了空格,变成abcxxx, abcqqq等等。我只知道用abc[^ ]可以搜索到,但是把空格插入到abc和后续单词中间的语法是怎样的?
-yesiam(party time);
2008-9-12
{147}
(#4683568@0)
-
what r u using? Java? then use replace()
-heresy(arcadia);
2008-9-12
(#4683749@0)
-
什么语言都可以,java, c#, php...我要的是ex表达式的语法
-yesiam(party time);
2008-9-12
(#4683975@0)
-
if you use python, you can use sub().
-qwertyasd(大小);
2008-9-12
(#4683787@0)
-
/(abc)/\1 /s only work for certain REs
-canadiantire(轮胎 - Cornelius);
2008-9-12
(#4683901@0)
-
/abc([^ ])/abc \1/s only work for certain REs
-diresu(makeITwork);
2008-9-12
(#4683981@0)
-
newText = oldtext.Replace("abc”, "abc "). Replace("abc ", "abc ")repalce abc to abc[one space], then repace abc[two space] to abc[one space].
-deep_blue(BLUE);
2008-9-12
{76}
(#4684044@0)
-
sorry,才发现我刚才没说清楚。在文档里,出现abc的地方后面不一定都是字母,例如abc), abc", abc.等。我要用regExp的原因,是只要把空格插入到abc后面是字母的地方,如果abc后面是标点则忽略
-yesiam(party time);
2008-9-12
(#4684118@0)
-
Suppose you need insert a space when a letter is behind abc:// C#
string txt = ...;
Regex rex = new Regex("abc[a-zA-Z]");
MatchCollection matchResult = rex.Matches(txt);
List<string> list = new List<string>();
foreach (Match m in matchResult)
{ if (!list.Contains(m.Value))
{
list.Add(m.Value);
}
}
foreach (string match in list)
{
txt = txt.Replace(match, "abc " + match.Substring(3, 1));
}
-deep_blue(BLUE);
2008-9-12
{390}
(#4684398@0)
-
不指定语言没意义。不同语言对正则表达式支持程度是不一样的。用 sed: sed -e 's/abc\([a-zA-Z]\+\)/aaa\1/' test.txt
-holdon(again);
2008-9-12
(#4684873@0)
-
You are right. Although there is no big difference in regular expression searching, there is big difference in regular expression replacing by different languages. Even with .NET there is difference in different FWs.For example, in .NET FW 3.5, you can easily archive it by
Regex reg = new Regex("abc[a-zA-Z]");
newText = reg.Replace(oldText, s => "abc " + s.Value.Substring(3,1));
-deep_blue(BLUE);
2008-9-13
{172}
(#4686382@0)