Mit94
Mit94

Reputation: 978

Extract numbers from a 1 line string using regex

I have a one line string that looks like this:

{"took":125,"timed_out":false,"_shards":{"total":10,"successful":10,"skipped":0,"failed":0}}{"took":365,"timed_out":false,"_shards":{"total":10,"successful":10,"skipped":0,"failed":0}}{"took":15,"timed_out":false,"_shards":{"total":10,"successful":10,"skipped":0,"failed":0}}

I would like to extract all the numbers after the "took" part, so in my case the output would look like this:

125
365
15

What I've tried so far is using took":(\d{1,6}),"(.*) as a regex. But since its a one line string, it only extracts the first occurence and ignores the others.

Upvotes: 2

Views: 595

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

You can use

Find What:      took":(\d+)|(?s)(?:(?!took":\d).)*
Replace With: (?{1}$1\n)

Details:

  • took": - literal text
  • (\d+) - one or more digits captured into Group 1
  • | - or
  • (?s) - set the DOTALL mode on (. matches line break chars now)
  • (?:(?!took":\d).)* - any single char, zero or more times, as many as possible, that does not start a took": + digit char sequence.

The (?{1}$1\n) conditional replacement pattern replaces this way:

  • (?{1} - if Group 1 is matched
    • $1\n - replace the match with Group 1 and a newline
  • ) - else, replace with an empty string.

Upvotes: 2

Related Questions