2017-03-01 6 views
1

これは簡単な質問ですが、私はまだグループの周りに頭を浮かべています。RegEx - グループ、文字列の必要[this:andthis]

私はこの文字列を持っています:this is some text [propertyFromId:34] and this is more textと私はもっとそれらのようになります。大括弧の間の内容を取得し、その後、コロンの左側にアルファのみのテキストを、コロンの右側に整数でグループ化する必要があります。

ので、完全一致:propertyFromId:34、グループ1:propertyFromId、グループ2:34

これは(?<=\[)(.*?)(?=])

答えて

0

使用

\[([a-zA-Z]+):(\d+)] 

regex demo

を参照してください私の出発点であります詳細

  • \[から[シンボル
  • ([a-zA-Z]+) - グループ1一つ以上のアルファ文字捕捉([[:alpha:]]+又は\p{L}+をも使用することができる)
  • : - 結腸
  • (\d+) - グループ2捕捉1つまたは複数の数字
  • ] - 閉鎖]シンボル。

PHP demo

$re = '~\[([a-zA-Z]+):(\d+)]~'; 
$str = 'this is some text [propertyFromId:34] and this is more text'; 
preg_match_all($re, $str, $matches); 
print_r($matches); 
// => Array 
// (
//  [0] => Array 
//   (
//    [0] => [propertyFromId:34] 
//   ) 
// 
//  [1] => Array 
//   (
//    [0] => propertyFromId 
//   ) 
// 
//  [2] => Array 
//   (
//    [0] => 34 
//   ) 
// 
// ) 
関連する問題