2010-03-03 49 views
3

我根本不知道正則表達式。任何人都可以用一個非常簡單的正則表達式來幫助我:正則表達式,以匹配與冒號連接的單詞對

從句子中提取'word:word'。例如「Java教程格式:Pdf With 位置:東京 Javascript」?

  • 很少修改: 第一個'單詞'是從列表中,但第二個是任何東西。 「[ABC,FGR,HTY] word1」
  • 傢伙情況要求多一點 修改。 匹配形式可以是「word11:word12 word13 ..」,直到下一個「word21:...」。

事情變得複雜與秒.....我要學習REG EX :(

在此先感謝

回答

5

您可以使用正則表達式:

\w+:\w+ 

說明:
\w - 字母(大寫或小寫),數字或_的單個字符。
\w+ - 一種或多種以上char..basically一個字的

所以\w+:\w+ 將匹配一對由冒號分隔的單詞。

2

嘗試\b(\S+?):(\S+?)\b。組1將捕獲「格式」和組2,「Pdf」。

工作示例:

<html> 
<head> 
<script type="text/javascript"> 
function test() { 
    var re = /\b(\S+?):(\S+?)\b/g; // without 'g' matches only the first 
    var text = "Java Tutorial Format:Pdf With Location:Tokyo Javascript"; 

    var match = null; 
    while ((match = re.exec(text)) != null) { 
     alert(match[1] + " -- " + match[2]); 
    } 

} 
</script> 
</head> 
<body onload="test();"> 

</body> 
</html> 

爲正則表達式一個很好的參考是https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp

+0

對於完整的示例+1,但對於一個正則表達式來說太複雜了-1。 ;) – 2010-03-03 11:46:37

-2
([^:]+):(.+) 

含義:(一切,除了:一次或多次),:,(任意字符一個或多個時間)

你會發現在網絡上很好的手冊......也許是時候讓你學習......

+0

不起作用:接受這個簡單的輸入:「ab cd:ef gh」你會匹配'ab cd'和'ef gh'而不是'cd'和'ef' – codaddict 2010-03-03 11:23:18

+0

這個正則表達式是非常錯誤的,你可能會也要善用手冊。 – 2010-03-03 11:48:12

+0

不明白,對不起。但是正則表達式起作用,你只需要按照以下方式調整它:([^:\ s] +):([^ \ s] +) – Macmade 2010-03-03 12:37:44

1

使用這段代碼:

 
$str=" this is pavun:kumar hello world bk:systesm" ; 
if (preg_match_all ('/(\w+\:\w+)/',$str ,$val)) 
{ 
print_r ($val) ; 
} 
else 
{ 
print "Not matched \n"; 
} 
-1

這裏的非正則表達式的方式,在你最喜歡的語言,在白色的空間分割,經過元,檢查「:」打印出來,如果發現。例如Python的

>>> s="Java Tutorial Format:Pdf With Location:Tokyo Javascript" 
>>> for i in s.split(): 
...  if ":" in i: 
...   print i 
... 
Format:Pdf 
Location:Tokyo 

你可以做進一步的檢查,以確保其真正「someword:someword」通過再次分裂「:」如果有在分裂列表2個元素檢查。例如

>>> for i in s.split(): 
...  if ":" in i: 
...   a=i.split(":") 
...   if len(a) == 2: 
...    print i 
... 
Format:Pdf 
Location:Tokyo 
1

繼續洪昭光與您的額外需求功能:

function test() { 
    var words = ['Format', 'Location', 'Size'], 
      text = "Java Tutorial Format:Pdf With Location:Tokyo Language:Javascript", 
      match = null; 
    var re = new RegExp('(' + words.join('|') + '):(\\w+)', 'g'); 
    while ((match = re.exec(text)) != null) { 
     alert(match[1] + " = " + match[2]); 
    } 
} 
0

我目前解決我的NodeJS應用程序,問題和發現這是,我猜,適用於結腸配對的字眼:

([\w]+:)("(([^"])*)"|'(([^'])*)'|(([^\s])*)) 

它也符合引用值。像a:"b" c:'d e' f:g

例編碼器ES6:

const regex = /([\w]+:)("(([^"])*)"|'(([^'])*)'|(([^\s])*))/g; 
const str = `category:"live casino" gsp:S1aik-UBnl aa:"b" c:'d e' f:g`; 
let m; 

while ((m = regex.exec(str)) !== null) { 
    // This is necessary to avoid infinite loops with zero-width matches 
    if (m.index === regex.lastIndex) { 
     regex.lastIndex++; 
    } 

    // The result can be accessed through the `m`-variable. 
    m.forEach((match, groupIndex) => { 
     console.log(`Found match, group ${groupIndex}: ${match}`); 
    }); 
} 

例在PHP編碼

$re = '/([\w]+:)("(([^"])*)"|\'(([^\'])*)\'|(([^\s])*))/'; 
$str = 'category:"live casino" gsp:S1aik-UBnl aa:"b" c:\'d e\' f:g'; 

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0); 

// Print the entire match result 
var_dump($matches); 

您可以檢查/使用這個在線工具測試您的正則表達式的表達式:https://regex101.com

順便說一句,如果沒有被regex101.com刪除,你可以瀏覽那個例子編碼here