0

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
Thomas Geulen
  • 590
  • 7
  • 22
  • 2
    change "word" char `\w` to `\S`, which is non-space. – karakfa May 31 '18 at 02:15
  • . . . . . . . . . . . . . . . . . . . . to `[\w.]+` or better `\d+([.]\d+)?` – wp78de May 31 '18 at 02:17
  • Possible duplicate of [Decimal number regular expression, where digit after decimal is optional](https://stackoverflow.com/questions/12117024/decimal-number-regular-expression-where-digit-after-decimal-is-optional) – wp78de May 31 '18 at 02:21

5 Answers5

1

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

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

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

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

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

0

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

\s?(\w+)=(\w+\.?\w+)?\s?
Boergler
  • 191
  • 1
  • 5
0

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.

Eddy
  • 67
  • 7
0

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?

The fourth bird
  • 154,723
  • 16
  • 55
  • 70