I would to get sting with parsing txt file, my string is:
[INFO] Total time: 12 minutes 10 seconds
I need is only "12 minutes 10 seconds"
my expression is:
/Total time: [0-9]* minute[a-z]* [0-9]* second[a-z]*/
it works perfectly, but if sting is only 1 minute it does not work...
how to get all sting after "Total time:" ?
asked Nov 20, 2011 at 11:15
Roman Iuvshin
1,91210 gold badges26 silver badges40 bronze badges
-
that's not a very useful comment, @EsailijaKae Verens– Kae Verens2011年11月20日 11:22:07 +00:00Commented Nov 20, 2011 at 11:22
-
@KaeVerens, I swear I saw exactly the same question, regex, and string to match just a few days ago :D Cannot find it though, but it doesn't get more duplicate than that :PEsailija– Esailija2011年11月20日 11:23:55 +00:00Commented Nov 20, 2011 at 11:23
-
@Roman - if you need the part starting with "12" (in your example), why is your regex including "Total time: "? Do you want the words "Total time: " in your output or not?nnnnnn– nnnnnn2011年11月20日 11:57:00 +00:00Commented Nov 20, 2011 at 11:57
3 Answers 3
Try this:
/Total time: ([0-9]* minutes?)?( ?[0-9]* seconds?)*/
You need a ? to match 0 or 1 preceding.
answered Nov 20, 2011 at 11:19
Abdul Munim
19.2k8 gold badges55 silver badges61 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Grrbrr404
I like your solution more then my own: Total time: [0-9]* minute[a-z]* [0-9]* second[a-z]*|[0-9]* minute[a-z]*
Roman Iuvshin
great! but.. what about if string contain only seconds? for ex. Total time: 22 sedonds?
Abdul Munim
There's a
space matching was missing. See the updated expressionYou could turn that seconds part into a subgroup and make it optional via the '?'
/Total time: [0-9]* minutes?( [0-9]* seconds?)?/
answered Nov 20, 2011 at 11:22
krichard
3,70427 silver badges35 bronze badges
1 Comment
nnnnnn
+1 for being the first person to fix the optional "s" in "minutes" and "seconds".
let's say the line is line
line=line.replace(/^\[INFO\] Total time: /, '');
answered Nov 20, 2011 at 11:21
Kae Verens
4,0773 gold badges25 silver badges43 bronze badges
Comments
lang-js