0

I am still learning the intrinsics of regex, and am wondering if it is possible with a single regex to find a number that is at a provided distance from a word.

Consider the following text

DateClient
15-01-20130060 15-01-20140010 15-01-20150020

I want that my regex matches just 15-01-2013.

I know I can have the full DateClient 15-01-2013 with DateClient\W+\d{2}-\d{2}-\d{4}, and then apply a regex afterwards, but i'm trying to build a configurable agnostic system, that gives power to the user, and so I would like to have a single regex expression that just matches 15-01-2013.

Is this even feasible?

Any suggestions?

4

2 に答える 2

2

キャプチャグループを使用できます:

DateClient\W+(\d{2}-\d{2}-\d{4})

javascriptの例(言語を指定していません):

var str = "DateClient\n15-01-20130060 15-01-20140010 15-01-20150020";
var date = str.match(/DateClient\W+(\d{2}-\d{2}-\d{4})/)[1];

編集(Rubyタグの追加後):

Rubyでは使用できます

(?<=DateClient\W)(\d{2}-\d{2}-\d{4})

デモンストレーション

于 2013-02-11T12:31:36.807 に答える
1

日付のみを一致させるために後読みをチェックしてください。ただし、ご使用の環境のルックビハインドサポートは制限される場合があります。

または、一致結果から抽出できるキャプチャグループを使用することもできます。

于 2013-02-11T12:33:07.170 に答える