ThinkTeamwork
ThinkTeamwork

Reputation: 594

Return first Number of each row?

I have this text and I want to replace each line with the first number that occurs in the line.

E.g.:

Affiliate,"0,00 €","1,13","0,00 €","0,00 €","0,00 %"
Bing - brand,"45,11 €","0,31","145,98 €","0,00 €","0,00 %"
Bing - nonbrand,"39,90 €","0,00","0,00 €","0,00 €","0,00 %"

Would become:

0,00
45,11
39,90

Can you help me on that?

My so far regex is:

(.*),"(.*),"(.*),"(.*),"(.*),"(.*)

with output $2

But that looks terrible and also doesn't give me the wanted result.

Upvotes: 0

Views: 48

Answers (2)

Gilles Quénot
Gilles Quénot

Reputation: 185640

Try this regex::

^.*?"(\d+\,\d+).*
$1

The regular expression matches as follows:

Node Explanation
^ the beginning of the string
.*? any character except \n (0 or more times (matching the least amount possible))
" "
( group and capture to \1:
\d+ digits (0-9) (1 or more times (matching the most amount possible))
\, ,
\d+ digits (0-9) (1 or more times (matching the most amount possible))
) end of \1
.* any character except \n (0 or more times (matching the most amount possible))

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522551

You may try the following find and replace, in regex mode:

Find:    ^\D*(\d+(?:,\d+)?).*$
Replace: $1

Here is an explanation of the regex pattern:

  • ^ from the start of the line
  • \D* consume zero or more non digit characters
  • (\d+(?:,\d+)?) match and capture in $1 the first digit, with optional decimal
  • .* consume the rest of the line
  • $ end of the line

Here is a working demo.

Upvotes: 2

Related Questions