0

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:" ?

3
  • that's not a very useful comment, @Esailija Commented 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 :P Commented 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? Commented Nov 20, 2011 at 11:57

3 Answers 3

2

Try this:

/Total time: ([0-9]* minutes?)?( ?[0-9]* seconds?)*/

You need a ? to match 0 or 1 preceding.

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

3 Comments

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]*
great! but.. what about if string contain only seconds? for ex. Total time: 22 sedonds?
There's a space matching was missing. See the updated expression
2

You could turn that seconds part into a subgroup and make it optional via the '?'

/Total time: [0-9]* minutes?( [0-9]* seconds?)?/

1 Comment

+1 for being the first person to fix the optional "s" in "minutes" and "seconds".
1

let's say the line is line

line=line.replace(/^\[INFO\] Total time: /, '');

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.