2011-12-12 35 views
1

我有一個像「test」的字符串模式:「abc/gef fhhff/fhrhr krerjr」,如何獲得並替換以下任務?

我想以加號「test」開頭的行中的所有空格。如何使用正則表達式來做到這一點?

+0

由於正則表達式本身沒有替換能力,它取決於你使用的是什麼工具。 Perl的? Java的? C#?桑達?埃德? – ibid

+0

* [user705414](http://stackoverflow.com/users/705414/user705414)*:您應該格式化代碼(本例中爲字符串),因爲不清楚引號是否是字符串的一部分。 –

回答

0

VB的淨嘗試

 Dim strCurrentLine as string = "test : abc/gef fhhff/fhrhr krerjr" 

     If UCase(strCurrentLine.ToString.StartsWith("TEST")) = True Then 
      strCurrentLine = Replace(strCurrentLine.ToString, " ", "Replace space with this text") 
     End If 

爲C#嘗試

string strCurrentLine = "test : abc/gef fhhff/fhrhr krerjr"; 

    if (Strings.UCase(strCurrentLine.ToString().StartsWith("TEST")) == true) { 
     strCurrentLine = Strings.Replace(strCurrentLine.ToString(), " ", "Replace space with this text"); 
    } 

或去這裏的VB的正則表達式http://visualbasic.about.com/od/usingvbnet/a/RegExNET_2.htm

或去這裏爲C#正則表達式http://oreilly.com/windows/archive/csharp-regular-expressions.html

0

在Perl中,你必須做一些事情,如:

use strict; 
use warnings; 

use 5.010; 

my $text = q|test : "abc/gef fhhff/fhrhr krerjr"|; 

#below is the regex that does the replace - \s is for whitespace class 
$text =~ s/\s/+/gmix if ($text =~ /^test/gmix); 

say "new text: $text";