1

私は、常に同じタイプのデータを持つ特定のテーブル列があるという問題を抱えています。検証のために、パターン一致に基づいてそのデータを検証するのが最も簡単だと思いました。

データセットの例:

*12 days ago
*1 minutes ago
*5.8 hours ago
*3.2 years ago

(*を無視してください)これが私が思いついた正規表現ですが、少しずれているように感じます:

String f = "^(?:\\d+|\\d*\\.\\d+)\\s+(\\byears|months|days|hours|minutes\\b)\\s+    (\\bago\\b)$";
Pattern p = p.compile(f);
Matcher m; 

if (m.find(retreiveRow(5))) { ...... }

どんな援助も素晴らしいでしょう!どうもありがとう!

4

2 に答える 2

1

サンプルデータ:

12 days ago
1 minutes ago
5.8 hours ago
3.2 years ago

私の正規表現:

/^([\d]+(?:\.\d)?)\s(years|months|days|hours|minutes)/

  (..............)  (...............................)

説明:

^([\d]+                              # match one or more digits
(?:\.\d)?)                           # followed by an optional period and digit
\s                                   # followed by a whitespace character
(years|months|days|hours|minutes)    # followed by a unit-of-time word

正規表現の下にある 2 つの括弧のペアは、正規表現に組み込まれた2 つのキャプチャ グループ(後方参照) を示しています。

あなたの質問はJavaに関するものですが、これはPerlを使用したデータに対するこの正規表現のライブデモです。Perlコードも参照用にここにあります:

#!/usr/bin/perl -w

use strict;
use warnings;

my @buf;
while (<DATA>) {
    @buf = /^([\d]+(?:\.\d)?)\s(years|months|days|hours|minutes)/;
    print "[", join("][", @buf), "]\n";
}

__DATA__
12 days ago
1 minutes ago
5.8 hours ago
3.2 years ago

出力:

[12][days]
[1][minutes]
[5.8][hours]
[3.2][years]
于 2013-06-06T02:56:47.683 に答える