3

文章中の単語や数字で書かれた数字を抽出する必要があります。

こんな感じのテーブルがありますが、

... 1 child ...
... three children ... 
...four children ...    
...2 children...
...five children

文字または数字で書かれた数字をキャプチャしたい。1 行に 1 つの数字があります。したがって、望ましい出力は次のようになります。

1
three
four
2
five

私の正規表現は次のようになります。

prxparse("/one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|child|\d\d?/")

助けはありますか?

4

1 に答える 1

6

説明

この正規表現は、数字が空白または記号で囲まれている場合、文字列内の数字と一致します。

(?<=\s|^)(?:[0-9]+|one|two|three|four|five|six|seven|eight|nine|ten)(?=\s|$)

ここに画像の説明を入力

実際の例: http://www.rubular.com/r/6ua7fTb8IS

1 ~ 10 以外の数字のスペルアウトされた単語バージョンを含めるには、それらを含める必要があります。この正規表現は、0 から 100 までの数字をキャプチャします [タイプミスを除く]

(?<=\s|^)(?:[0-9]+|(?:(?:twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety)\s)?(?:one(?:[\s-]hundred)?|two|three|four|five|six|seven|eight|nine)|ten|eleven|twelve|(?:thir|four|fif|six|seven|eight|nine)teen|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety|zero)(?=\s|$)

ここに画像の説明を入力

実際の例: http://www.rubular.com/r/EIa18nx731

Perl の例

 $string = <<END;
 ... 1 child ...
 ... three children ... 
 ... four children ...    
 ... 2 children...
 ... five children
END
@matches = $string =~ m/(?<=\s|^)[0-9]+|one|two|three|four|five|six|seven|eight|nine|ten(?=\s|$)/gi;
    print join("\n", @matches);

収量

1
three
four
2
five
于 2013-06-12T16:20:03.007 に答える