私は擬似コードで正規表現の知識を持っていますが、私はPHP regex perlでやりたいことを翻訳するのに困っています。
私は式の一部を抽出するためにpreg_matchを使用しようとしています。
は、私は次の文字列${classA.methodA.methodB(classB.methodC(classB.methodD)))}
を持っていると私は2つの物事実行する必要があります。
PHP Regex preg_matchの抽出
。
${classA.methodA.methodB(classB.methodC(classB.methodD)))}
有効な構文を検証${classA.methodA.methodB}
有効${classA.methodA.methodB()}
有効ではありません${methodB(methodC(classB.methodD)))}
有効ではありません
b。私は、私が作成した
2にmethodA
3. methodB(classB.methodC(classB.methodD)))
を、それらの情報 ${classA.methodA.methodB(classB.methodC(classB.methodD)))}
を抽出する必要が
1.クラスAを返す必要がありますこのコードは
$expression = '${myvalue.fdsfs.fsdf.blo(fsdf.fsfds(fsfs.fs))}';
$pattern = '/\$\{(?:([a-zA-Z0-9]+)\.)(?:([a-zA-Z\d]+)\.)*([a-zA-Z\d.()]+)\}/';
if(preg_match($pattern, $expression, $matches))
{
echo 'found'.'<br/>';
for($i = 0; $i < count($matches); $i++)
echo $i." ".$matches[$i].'<br/>';
}
結果は次のとおりです。
は
を見つけました 0 $ {myvalue.fdsfs.fsdf.blo(fsdf.fsfds(fsfs.fs))}
1 myvalue
2 fsdf
3 BLO(fsdf.fsfds(fsfs.fs))
明らか
私は反復メソッドを抽出するのが難しいですし、正当に検証していません(正直なところ、他の問題を解決したら最後に残しました)ので、空のかっこが許され、かっこが開かれたかどうかをチェックしません閉じなければならない。
おかげであなたの助けのためのすべての
UPDATE
X m.buettner
感謝。私は速くあなたのコードを試しましたが、それは非常に小さな問題を与えますが、私はそれを渡すことができます。問題は、私はこの文字列をしようとしているここに投稿していない私の前にコードのいずれかの同じである:あなたのパターン定義と
$expression = '${myvalue.fdsfs}';
それは示しています
found
0 ${myvalue.fdsfs}
1 myvalue.fdsfs
2 myvalue
3
4 fdsfs
することができますように3行目が存在しない空白としてキャッチされていることを参照してください。私はそれがなぜそうしていたのか理解できませんでしたので、PHPの正規表現の限界のためにどうやってやっていかなければならないのかを教えてください。
私はちょうどあなたに感謝を伝えることができると言いました。あなたは私の問題に答えただけでなく、できるだけ多くの情報を入力して、パターンを開発するときの適切な道筋について多くの提案をしました。
最後に一つは、私は(愚かな)ので
$expression = '${classA.methodAA(classB.methodBA(classC.methodCA),classC.methodCB)}';
$expression = '${classA.methodAA(classB.methodBA(classC.methodCA),classC.methodCB,classD.mehtodDA)}';
が有効である必要があり、コンマで割った複数のパラメータである一つの小さな重要なケースを追加するのを忘れ。
は私も良いのですが、このコードを使用しているとき、それは少し複雑な状況を意味し、この$expressionPattern =
'/
^ # beginning of the string
[$][{] # literal ${
( # group 1, used for recursion
( # group 2 (class name)
[a-z\d]+ # one or more alphanumeric characters
) # end of group 2 (class name)
[.] # literal .
( # group 3 (all intermediate method names)
(?: # non-capturing group that matches a single method name
[a-z\d]+ # one or more alphanumeric characters
[.] # literal .
)* # end of method name, repeat 0 or more times
) # end of group 3 (intermediate method names);
( # group 4 (final method name and arguments)
[a-z\d]+ # one or or more alphanumeric characters
(?: # non-capturing group for arguments
[(] # literal (
(?1) # recursively apply the pattern inside group 1
(?: # non-capturing group for multiple arguments
[,] # literal ,
(?1) # recursively apply the pattern inside group 1 on parameters
)* # end of multiple arguments group; repeat 0 or more times
[)] # literal)
)? # end of argument-group; make optional
) # end of group 4 (method name and arguments)
) # end of group 1 (recursion group)
[}] # literal }
$ # end of the string
/ix';
Xカシミールらイポリット
あなたの提案に編集しました。つまり、コード自体は理解しやすいが、柔軟性が低くなるということだ。それはまた、私に将来的に役立つことができる多くの情報を与えてくれたと言っています。
X Denomales
ご支援のおかげで、私はこれをしようとすると、コードが落ちる:
$sourcestring='${classA1.methodA0.methodA1.methodB1(classB.methodC(classB.methodD))}';
結果は次のとおりです。
Array
( [0] =>配列 ( [0] => $ {classA1.methodA0.methodA1.methodB1(classB.methodC(cl assB.methodD))} )
[1] => Array
(
[0] => classA1
)
[2] => Array
(
[0] => methodA0
)
[3] => Array
(
[0] => methodA1.methodB1(classB.methodC(classB.methodD))
)
)
それはあなたが同じパターン、例を検証し、抽出を行うことができます
[2] => Array
(
[0] => methodA0.methodA1
)
[3] => Array
(
[0] => methodB1(classB.methodC(classB.methodD))
)
)
または
[2] => Array
(
[0] => methodA0
)
[3] => Array
(
[0] => methodA1
)
[4] => Array
(
[0] => methodB1(classB.methodC(classB.methodD))
)
)
私はこの$式= '$ {myvalue.fdsfsを}' してみてください。 – user2463968
@ user2463968それでは?クラス名を取得し、中間メソッドも引数も持たない最終メソッドを取得します。それはあなたの意図ではありませんか? –