0

Need find all the text to "min" inclusive.

All online services and "subline text 3" are correct found, but the "match" returns null. Help me please

ps: sorry for my english

JavaScript:

var tmp = data.match(/(\[Rec\].+?),(.+?),(.+?),(\d+),((.|\n)*?min)/ig);

String:

[Rec] name,2016-01-13 00:51:22.447,2015-05-11 00:21:52.497,1,[ 2016.09.11 21:14:56 ] name > some text
[ 2016.09.11 21:19:36 ] name : some text
[ 2016.09.11 21:19:48 ] name : some text
[ 2016.09.11 21:20:07 ] name : some text

30 mins

[Rec] name,2016-09-10 13:55:23.117,2016-04-11 11:51:23.117,1,[ 2016.09.09 20:52:56 ] name > text
[ 2016.09.09 20:53:05 ] name : some text
[ 2016.09.09 20:53:43 ] name : some text
[ 2016.09.09 20:54:23 ] name : some text

40 minutes

expected output:

tmp[1] = [Rec] name
tmp[2] = 2016-01-13 00:51:22.447
tmp[3] = 2015-05-11 00:21:52.497
tmp[4] = 1
tmp[5] = [ 2016.09.11 21:14:56 ] name > some text [ 2016.09.11 21:19:36 ] name : some text [ 2016.09.11 21:19:48 ] name : some text [ 2016.09.11 21:20:07 ] name : some text
30 min
4
  • can you show us an example of expected output? Commented Feb 24, 2017 at 20:35
  • done, sry first use this site Commented Feb 24, 2017 at 20:47
  • You should never use (.|\n)*?, always replace with [\s\S]*? Commented Feb 24, 2017 at 20:58
  • @WiktorStribiżew Thx! its help ! Commented Feb 24, 2017 at 21:04

2 Answers 2

1

You want to use Regex.exec(String) instead of String.match(Regex).

Here is a jsfiddle, open the console to see the output.

https://jsfiddle.net/qwaxhk1g/1/

Sign up to request clarification or add additional context in comments.

Comments

0

It is highly possible you also have carriage returns in your file, and . in JS regex does not match them. You should replace the (.|\n)*? with a much more efficient [\s\S]*? (or [^]*?, though it will only ever work in JS regex).

Also, you seem to need to access capturing group contents, and when you use /g modifier, you need to use run RegExp#exec(String) in a loop to grab all matches together with captures.

var rx = /(\[Rec\].+?),(.+?),(.+?),(\d+),([\s\S]*?min)/ig;
var s = "[Rec] name,2016-01-13 00:51:22.447,2015-05-11 00:21:52.497,1,[ 2016.09.11 21:14:56 ] name > some text\n[ 2016.09.11 21:19:36 ] name : some text\n[ 2016.09.11 21:19:48 ] name : some text\n[ 2016.09.11 21:20:07 ] name : some text\n\n30 mins\n\n[Rec] name,2016-09-10 13:55:23.117,2016-04-11 11:51:23.117,1,[ 2016.09.09 20:52:56 ] name > text\n[ 2016.09.09 20:53:05 ] name : some text\n[ 2016.09.09 20:53:43 ] name : some text\n[ 2016.09.09 20:54:23 ] name : some text\n\n40 minutes";
while ((m=rx.exec(s)) !== null) {
  console.log("Current match:");
  console.log(m[1]);
  console.log(m[2]);
  console.log(m[3]);
  console.log(m[4]);
  console.log(m[5]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.