2016-06-15 16 views
1

マッチの発生一致していることを確認するにはどうすればよい:あなたの正規表現が、私はPCRE(PHP)正規表現を使っていますし、次の正規表現開発した

(?:-?)(?:[A-Z’\s\.-]{8}.*)(?:NY\s?|ON\s?|FL\s){1}([A-Z].*)(?:M\s\d{1,2}\s.*|F\s\d{1,2}.*) 

Iを適用しようとしています次の文字列。代わりにマッチの最初の発生時に停止するので、正規表現文字列の多くを消費し、第二のマッチの発生と一致されるそれぞれの場合において

SUNDAY GEISHA-SUNDAY BREAK-JP NYHIT IT ONCE MORE M 13 1116 Race 1 
Desired Match: HIT IT ONCE MORE 
Actual Match: CE MORE 

LOAD UP-DOVE HUNT FL SUMMATION TIME M 11 6T Race 6 
Desired Match: SUMMATION TIME 
Actual Match: TIME 

TEMPLE STREET-STREET CRY-IR KY DONWORTH M 12 1 Race 9 
Desired Match: DONWORTH 
Actual Match: WORTH 

:各ターゲット文字列の下にIは、実際の試合に対する所望の一致を提供しています。

あなたはregex101.comでここで働い例を見ることができます:私は私の所望の出力を達成して、私が最初に一致した時点で停止するように私の正規表現を取得するにはどうすればよいWORKING EXAMPLE

?私は自分の表現をどのように改善できるかについての指針を歓迎します。

ありがとうございます。

+1

たぶん、あなたは 'NY'と一致したときに、' ON "、" FL "の前にスペースや' -'が付いていることを確認する必要がありますが、それらの文字で始まるフレーズには問題があります。私はこの問題は単なる正規表現では解決できないと考えています。それを正しく100%するには英単語の辞書が必要です。 – Mike

+0

@Mikeあなたは英語の単語の辞書を言うときはどういう意味ですか?あなたは、あなたが何を指しているかの例に私を導くことができますか? – Mutuelinvestor

+1

私は、一般に、「NYHIT」が言葉ではないことを知っていない限り、あなたが望むことをする方法がないことを意味します。 – Mike

答えて

1

説明

^(?:[^ \n]* +){4}(.*?) +[a-z] +[0-9]+ [0-9a-z]+ Race [0-9]+$ 

Regular expression visualization

ライブデモ

https://regex101.com/r/kF9cU8/2

サンプルテキスト

SUNDAY GEISHA-SUNDAY BREAK-JP NY HIT IT ONCE MORE M 13 1116 Race 1 
Desired Match: HIT IT ONCE MORE 
Actual Match: CE MORE 

LOAD UP-DOVE HUNT FL SUMMATION TIME M 11 6T Race 6 
Desired Match: SUMMATION TIME 
Actual Match: TIME 

TEMPLE STREET-STREET CRY-IR KY DONWORTH M 12 1 Race 9 
Desired Match: DONWORTH 
Actual Match: WORTH 

サンプルは説明

MATCH 1 
1. [33-49] `HIT IT ONCE MORE` 

MATCH 2 
1. [145-159] `SUMMATION TIME` 

MATCH 3 
1. [258-266] `DONWORTH` 

NODE      EXPLANATION 
---------------------------------------------------------------------- 
^      the beginning of a "line" 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (4 times): 
---------------------------------------------------------------------- 
    [^ \n]*     any character except: ' ', '\n' 
          (newline) (0 or more times (matching the 
          most amount possible)) 
---------------------------------------------------------------------- 
    +      ' ' (1 or more times (matching the most 
          amount possible)) 
---------------------------------------------------------------------- 
){4}      end of grouping 
---------------------------------------------------------------------- 
    (      group and capture to \1: 
---------------------------------------------------------------------- 
    .*?      any character except \n (0 or more times 
          (matching the least amount possible)) 
---------------------------------------------------------------------- 
)      end of \1 
---------------------------------------------------------------------- 
    +      ' ' (1 or more times (matching the most 
          amount possible)) 
---------------------------------------------------------------------- 
    [a-z]     any character of: 'a' to 'z' 
---------------------------------------------------------------------- 
    +      ' ' (1 or more times (matching the most 
          amount possible)) 
---------------------------------------------------------------------- 
    [0-9]+     any character of: '0' to '9' (1 or more 
          times (matching the most amount possible)) 
---------------------------------------------------------------------- 
          ' ' 
---------------------------------------------------------------------- 
    [0-9a-z]+    any character of: '0' to '9', 'a' to 'z' 
          (1 or more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    Race     ' Race ' 
---------------------------------------------------------------------- 
    [0-9]+     any character of: '0' to '9' (1 or more 
          times (matching the most amount possible)) 
---------------------------------------------------------------------- 
    $      before an optional \n, and the end of a 
          "line" 
---------------------------------------------------------------------- 
1

まあ、simplier(しかし、より効率的ではない)に一致aproach:

/^.+(?:NY|FL|KY)\s?(.+?)(?: M.*)$/gmi 

がもたらす:

  1. を "ONCE ITを打つ" "SUMMATION TIME"
  2. は "DONWORTH"

はそれを試してみてください。https://regex101.com/r/yX2bI1/4

+1

@Ro Yo Miが3倍少ないステップで仕事をすることを考慮してください。ハハ – Jaumzera

関連する問題