Thomas Geulen
Thomas Geulen

Reputation: 610

Regex is cutting my string at a dot

I have the following regular expression:

\s?(\w+)=(\w+)?\s?

My input is

Brutto=57.800

The match is just

Brutto=57

What can I do to get the following result?

Brutto=57.800

Upvotes: 0

Views: 158

Answers (5)

The fourth bird
The fourth bird

Reputation: 163632

In your regex you use \w+ which does not match the dot in 57.800 that so it would match 57.

If your goal is to match digits after the equals sign with an optional whitespace character \s? at the start and at the end, you might use:

\s?\w+=[0-9]+\.[0-9]+\s?

Note that I have not use captured groups (). If you want to retrieve your matches using groups, you could use (\w+) and ([0-9]+\.[0-9]+)

If the .800 is optional you could use \s?\w+=[0-9]+(?:\.[0-9]+)?\s?

Upvotes: 0

Bohemian
Bohemian

Reputation: 425418

I would just get all "non-space" chars:

\s?(\w+)=(\S*)\s?

FYI (\S*) has the same effect as (\S+)?, but is simpler.

Upvotes: 1

Eddy
Eddy

Reputation: 77

If u need just matching u can try \s?\w+=\d+.\d+\s?.

If u need grouping also u can try \s?(\w+)=(\d+.\d+)\s?

u can also replace \d with \w as \d represents only digits whereas \w represents any Alphanumeric characters.

Upvotes: 0

Boergler
Boergler

Reputation: 301

You must handle the dot separately (and escape it with a backslash):

\s?(\w+)=(\w+\.?\w+)?\s?

Upvotes: 0

You can use something like if the value always contains a decimal point \s?(\w+)=(\d+.\d+)?\s?

Upvotes: 0

Related Questions