0

正規表現を学ぼうとしています。私は基本を知っています、そして私は正規表現でひどいわけではありません、私はただプロではありません-それで私はあなたたちに質問があります。正規表現を知っているなら、きっと簡単でしょう。

私が現在持っているのはこれです:

/(\w+)\s-{1}\s(\w+)\.{1}(\w{3,4})/

私がやろうとしているのは、すべてのファイル名をフォーマットすることによって私の音楽コレクションを整理する小さなスクリプトを自分で作成することです。すでに他のものがあることは知っていますが、これは私にとって学習体験です。「地獄は楽しい所」のようなものを「地獄は楽しい所」に置き換えて、すでにすべてのタイトルを台無しにしました。私の知恵では、どういうわけか「Hell Aint a ad Place to be」(Aの後にスペースと大文字が続く)になってしまいました。明らかに、それは修正するのが悪夢であり、手動で行う必要がありました。言うまでもなく、私は今最初にサンプルをテストしています。

とにかく、上記の正規表現は、多くのステージ1のようなものです。最終的にはそれを構築したいのですが、今のところは単純なビットを機能させる必要があります。

最後に、私は向きを変えたいと思います:

"arctic Monkeys- a fake tales of a san francisco"

の中へ

"Arctic Monkeys - A Fake Tales of a San Francisco"

「-」の後にある場合は、後ろ向きのアサーションが必要になることはわかっています。最初の単語が「a」、「of」などで、通常は小文字である場合は、大文字にする必要があるためです(上記私が知っているこのユースケースの悪い例です)。

既存の正規表現を修正する方法はどれも素晴らしいでしょうし、残りを仕上げるために虎の巻をどこで見るかについてのヒントは素晴らしいでしょう(私はすることを学ぶ必要があるので、本格的な答えを探していませんそれ自体、なぜw +が1つの単語しか取得していないのか理解できません)。

4

5 に答える 5

1

\wには空白が含まれていません。有効な正規表現は次のようになります。

/^(.+?)\s*-\s*(.+)$/

説明:

^     - must start at the beginning of the string
(.+?) - match any character, be ungreedy
\s*   - match any number whitespace that might exists (including none)
-     - match character
\s*   - any whitespace again
(.+)  - remaining characters
$     - end of string

トランスコーディングは、別の置換正規表現で行われます。

于 2012-09-29T08:06:04.420 に答える
1

私はあなたがしていることについて少し混乱していますが、多分これは助けになるでしょう。+は1文字以上、*は0文字以上であることを忘れないでください。したがって、スペースを一致させるために([\ s] *)のようなことをしたいと思うでしょう。1文字の横に{1}を指定する必要はありません。

だから多分このようなもの:

([\w\s]+)([\s]*)-([\s]*)([\w\s]+)\.([\w]{3,4})

私はこのコードをテストしていませんが、あなたはその考えを理解していると思います。

于 2012-09-29T08:07:51.247 に答える
1

この問題に取り組むには、はるかに簡単な方法があると思います。はるかに単純な正規表現に基づいて文字列を単語に分割し、それらの単語に必要な処理を適用します。これにより、テキストに対してより複雑な変換をよりクリーンな方法で実行できるようになります。次に例を示します。

<?php

$song = "arctic Monkeys- a fake tales of a san francisco";

// Split on spaces or - (the - is still present
// because it's only a lookahead match)
$words = preg_split("/([\s]+|(?=-))/", $song);

/*
Output for print_r:
Array
(
    [0] => arctic
    [1] => Monkeys
    [2] => -
    [3] => a
    [4] => fake
    [5] => tales
    [6] => of
    [7] => a
    [8] => san
    [9] => francisco
)
*/
print_r($words);

$new_words = array();
foreach ($words as $k => $word) {
        $new_words[] = processWord($word, $k, $words);
}

// This will output:
// Arctic Monkeys - A Fake Tales of a San Francisco
echo implode(' ', $new_words);

// You can add as many processing rules you want in here - in a very clean way
function processWord($word, $idx, $words) {
        if ($words[$idx - 1] == '-') return ucfirst($word);
        return strlen($word) > 2 ? ucfirst($word) : $word;
}

このコードの実行例を次に示します。http://codepad.org/t6pc8WpR

于 2012-09-29T08:51:31.743 に答える
0

最初の部分では、\ wは単語と一致せず、単語の文字と一致します。[A-Za-z0-9_]と同等です。

代わりに、最初のビットとして([A-Za-z0-9_] +)を試してください(一致する角かっこ内に余分なスペースがあり、\sを削除しました。

于 2012-09-29T08:04:47.593 に答える
0

これが私が持っているものです:

<?php
/**
 * Formats a string into a title:
 * * Pads all dashes with spaces.
 * * Uppercase all words with 3 letters or more.
 * * Uppercase first word and first words after dashes.
 *
 * @param $str
 *
 * @return string
 */
function format_title($str) {
    //Remove all spaces before and after dashes.
    //(These will return in the final product)
    $str = preg_replace("/\s?-\s?/", "-", $str);

    //Explode by dash.
    $string_split_by_dash = explode("-", $str);
    //For each sentence (separated by dashes)
    foreach ($string_split_by_dash as &$sentence) {
        //Uppercase all words.
        $sentence = ucwords($sentence);
        //Explode into words (by space)
        $words = explode(" ", $sentence);
        //For each word
        foreach ($words as &$word) {
            //If its length is smaller than 3
            if (strlen($word) < 3) {
                //Lowercase it.
                $word = strtolower($word);
            }
        }
        //Implode back into a sentence.
        $sentence = implode(" ", $words);
        //Uppercase the first word, regardless of length.
        $sentence = ucfirst($sentence);
    }

    //Implode all sentances back by space-padded dash.
    $str = implode(" - ", $string_split_by_dash);

    return $str;
}

$str = "arctic Monkeys- a fake tales of a san francisco";
var_dump(format_title($str));

私はそれが正規表現よりも読みやすい(そしてより文書化できる)と主張したいと思います。おそらくもっと効率的です(チェックしませんでした)。

于 2012-09-29T08:52:13.470 に答える