2010-01-08 63 views

回答

2

你真的需要檢查一下字符串是否有'/'嗎?如果是的話,你可以考慮使用什麼可能是無論你在編寫代碼的語言內置方法:

bool containsSlash = myString.IndexOf('/') >= 0; 

否則,你可以「越獄」使用下列符號的字符;

\/ 
2

用反斜槓(例如, /

+0

請參閱我的編輯。 – 2010-01-08 15:58:24

1

用\(反斜槓)轉義它。

1

正則表達式API往往是特定於實現的,所以我們需要知道您使用哪種語言/工具來給出100%正確的答案。但很快一個很簡單

.*\/.* 

然而,對於這樣的問題,它更有效地使用字符串搜索API。正則表達式最適合匹配模式。過濾出單個字符最好通過IndexOf或類似的函數完成。

+1

的開頭和結尾。*沒有必要的大部分時間:) – KiNgMaR 2010-01-08 16:00:30

+1

這取決於,如果您的發動機從線的起點或「搜索」文本匹配。 *後的應該不重要。 – avpx 2010-01-08 16:01:39

0

在C#:

String sss = "<your string>"; 
Regex re1 = new Regex(@".*/.*"); 

if (re1.IsMatch(sss)) ..... 
0

你可能想\/+。然而這取決於引擎。

+0

對於這個特殊問題,+不是必需的。 – 2010-01-08 16:49:10

0

我會用僞Perl給你,因爲你沒有要求任何特定的東西。

$has_slash = 0; 
# first slash starts regex 
# backslash escape the next slash 
# because it is escaped, we're looking for this as a literal slash 
# end the regex pattern with the final slash 
if ($string =~ /\//) { 
    $has_slash = 1; 
} 
1

你不需要一個正則表達式,有更便宜的方法來掃描一個字符。如果您使用c,我建議您使用strrchr。從手冊頁:

char * strrchr(const char *s, int c); 

的strrchr()函數座落在字符串s C的 最後出現(轉換爲 炭)。如果c是\0', strrchr() locates the terminating \ 0'。

例如:

bool contains(char c, char* myString) { 
    return 0 != strrchr(myString, c); 
} 

contains("alex", 'x'); // returns true 
contains("woo\\123", '\\'); // returns true 
相關問題