0

preg_matches について少し質問があります。これらの正規表現は非常に理解しにくいので、誰かが正しい awnser を提供してくれることを願っています!

次のテキストがあります。

0A-24-423

しかし、これは次の場合もあります。

0A-242-2

また

0A-2-423

preg_matches を使用してこれらをフィルタリングするにはどうすればよいですか? 使っていました

substr($something, 0,2)

0Aをキャプチャし、

substr($seat, 4,5)

これは24をキャプチャしますが、242を取得すると最後の2をキャプチャしません....

誰かが preg_match でこれを作成するのを手伝ってくれることを願っています!

私が今持っているものをより明確にするために:

        foreach($_POST['seats'] AS $seat) {
        if ($count > 0) {
            $selectQuery .= " || ";
        }
        $selectQuery .= " ( rowId = '" . substr($seat, 0,2) . "'";
        $selectQuery .= " and `order` = " . substr($seat, 3,5) . "  ";
        $selectQuery .= " and columnId = " . substr($seat, 6) . " ) ";
        $count++;

$seat の形式は XXXXXX で、substr を使用すると適切なものを取得できます (例: 0J3017)

このような何かがそれを行う必要があります:

    $selectQuery = "SELECT * from seats where ";
    $count = 0;
$pattern = "I DON'T KNOW :( ";
    foreach($_POST['seats'] AS $seat) {
        if ($count > 0) {
            $selectQuery .= " || ";
        }
        preg_match($pattern, $seats, $matches);
        $selectQuery .= " ( rowId = '" . $matches[0] . "'";
        $selectQuery .= " and `order` = " . $matches[1] . "  ";
        $selectQuery .= " and columnId = " . $matches[2] . " ) ";
        $count++;

$seats は投稿の冒頭で説明されています (形式は次のとおりです)。XX-XXX-XXX

where the first 2 XX  are 0[A-Z] (yes the 0 is correct)
where the 3 first XXX are [0-9]
Where the last 3  XXX are [0-9]

編集:これを解決するには2つの方法があります。

オプション1:

$pattern = "/(.*)-(.*)-(.*)/";

または、explode() 関数を使用します。

4

2 に答える 2

2

正規表現を使用する必要はないようです。explode()と を使用した例を次に示しlist()ます。

list($row_id, $order, $column_id) = explode('-', $seat, 3);

その後、これら 3 つの新しい変数を で使用できます$selectQuery

于 2012-12-31T14:10:36.733 に答える
1

編集: OPが私の回答へのコメントとして彼の要件を述べているので、それに応じて回答を更新しました。

これを試すことができます:

$pattern = "/[A-Z\d]{1,2}-[A-Z\d]{1,3}-[A-Z\d]{1,3}/";
$matched = preg_match($pattern, $something);
if ($matched === 0) {
  die('regex did not match');
}

$matched1一致した文字列が表示され、一致しない0場合は表示されます。

于 2012-12-31T12:50:15.843 に答える