1

I'm trying to apply a regex to extract data from the subject of several mails (from a label). I only get a result every other mail? The regex is correct, I tested all subjects against the pattern with an online tool.

Anyone an idea what's wrong? I have tried a dozen things, but no luck.

Code

for (var i = 0; i < 5; i++) {
   var msg = threads[i].getMessages()[0];
   var body = msg.getBody();
   var subject = msg.getSubject();
   Logger.log("#" + subject + "#");
   var re = /Week (\d{1,2})\s?[-:]\s?(.*?)$/gi;
   var match = re.exec(subject);
   Logger.log(match);
   if (match) { 
      Logger.log(match[1] + '---' + match[2]);
   }
}

Log

[16-08-24 10:36:38:003 CEST] #Week 25 - Samosa aardappel koriander#
[16-08-24 10:36:38:004 CEST] [Week 25 - Samosa aardappel koriander, 25, Samosa aardappel koriander]
[16-08-24 10:36:38:005 CEST] 25---Samosa aardappel koriander
[16-08-24 10:36:38:156 CEST] #Week 24 - Pebre#
[16-08-24 10:36:38:156 CEST] null
[16-08-24 10:36:42:318 CEST] #Week 23 - Caramel chocolade shortbread#
[16-08-24 10:36:42:319 CEST] [Week 23 - Caramel chocolade shortbread, 23, Caramel chocolade shortbread]
[16-08-24 10:36:42:319 CEST] 23---Caramel chocolade shortbread
[16-08-24 10:36:42:491 CEST] #Week 22 - Bretzel#
[16-08-24 10:36:42:492 CEST] null
[16-08-24 10:36:42:674 CEST] #Week 21 - Basilicum quenelles#
[16-08-24 10:36:42:675 CEST] [Week 21 - Basilicum quenelles, 21, Basilicum quenelles]
[16-08-24 10:36:42:675 CEST] 21---Basilicum quenelles

When I start with i = 1 I get

[16-08-24 10:38:44:158 CEST] #Week 24 - Pebre#
[16-08-24 10:38:44:159 CEST] [Week 24 - Pebre, 24, Pebre]
[16-08-24 10:38:44:159 CEST] 24---Pebre
[16-08-24 10:38:44:307 CEST] #Week 23 - Caramel chocolade shortbread#
[16-08-24 10:38:44:307 CEST] null
[16-08-24 10:38:46:463 CEST] #Week 22 - Bretzel#
[16-08-24 10:38:46:463 CEST] [Week 22 - Bretzel, 22, Bretzel]
[16-08-24 10:38:46:463 CEST] 22---Bretzel
[16-08-24 10:38:46:616 CEST] #Week 21 - Basilicum quenelles#
[16-08-24 10:38:46:616 CEST] null

2 Answers 2

1

It is just a matter of using a global modifier or not in your case.

See

var re = /Week (\d{1,2})\s?[-:]\s?(.*?)$/gi;
var match = re.exec(subject);

Your regex contains a global modifier g that, when used with RegExp#exec, advances the RegExp lastIndex with each successful match.

If your regular expression uses the "g" flag, you can use the exec() method multiple times to find successive matches in the same string. When you do so, the search starts at the substring of str specified by the regular expression's lastIndex property (test() will also advance the lastIndex property).

Since you are using exec to only get one match at a time, the g modifier is redundant.

The solution is simply to remove g (and I'd suggest using * quantifier after \s to handle multiple whitespaces if any, and replace .*? with .* at the end to grab all the rest of the line in 1 go without checking each char):

var re = /Week (\d{1,2})\s*[-:]\s*(.*)/i;
Sign up to request clarification or add additional context in comments.

Comments

1

you need to reset the last index of the regex (this have been a really painfull for me to discover when I got the same trouble)

re.lastIndex = 0;  

Here your code with the modif:

for (var i = 0; i < 5; i++) {
   var msg = threads[i].getMessages()[0];
   var body = msg.getBody();
   var subject = msg.getSubject();
   Logger.log("#" + subject + "#");
   var re = /Week (\d{1,2})\s?[-:]\s?(.*?)$/gi;
   re. lastIndex = 0;
   var match = re.exec(subject);
   Logger.log(match);
   if (match) { 
      Logger.log(match[1] + '---' + match[2]);
   }
}

for further understanding have a look at this documentation

4 Comments

You need to reset the lastindex of the regex even if you re create the var at each loop (don't ask me why)
Thanks! You just save me a couple more hours of cursing ;-)
No need to reset anything, just use var re = /Week (\d{1,2})\s?[-:]\s?(.*?)$/i; - without /g. Then you can remove re.lastIndex = 0;. Just use what you need, no more, no less, and your code will stay efficient and succint.
@WiktorStribiżew you are right, in this case it's better to remove the "g"

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.