Avión
Avión

Reputation: 8396

Regex to extract size from a string

Having this input string:

health status index              pri rep docs.count docs.deleted store.size pri.store.size
yellow open   test1                 5   1     222173            0     43.8gb         43.8gb
yellow open   test2                 5   1      27142           10     36.6mb         36.6mb

I'm using the following regex to extract some info:

^(\w+)\W+\w+\W+(\w+)\W+\w+\W+(\w+)\W+(\w+)\W+(\w+)\W+(\w+)~m

As you can see, from the last group (Group 6) I'm just the number until the first dot (43).

How can I correct it so the last group takes the entire number? I mean, take 43.8 and 36.6 without the gb and mb?

Demo: https://regex101.com/r/rMfWC4/3

Thanks in advance

Upvotes: 1

Views: 163

Answers (2)

Manoj Kumar S
Manoj Kumar S

Reputation: 11

^(\w+)\W+\w+\W+(\w+)\W+\w+\W+(\w+)\W+(\w+)\W+(\w+)\W+(\w+\W+\d+)

Hi, your are almost there adding the non digit capture will capture the dot(.) or whatever special characters, and adding \d will capture the digit again + sign will capture the digits if it contains more than one. I hope this will helps you. Good Luck.

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627180

You may add an alternation (\d+\.\d+|\w+) to match either digit(s) + . + digit(s) OR (|) alphanum/_ chars:

^(\w+)\W+\w+\W+(\w+)\W+\w+\W+(\w+)\W+(\w+)\W+(\w+)\W+(\d+\.\d+|\w+)
                                                      ^^^^^^^^^^^^^

See the regex demo

Upvotes: 2

Related Questions