1

Learning regex but this one gives me a headache. I need to match a float number (with either . or , as decimal point) and it MUST end with the following characters: €/g.

Valid matches should be for example:

  • 40€/g
  • 43.33€/g
  • 40,2€/g
  • 40.2€/g
  • 38.943€/g

Appreciate help..

5
  • 3
    Which expressions have you tried so far? What exactly are you having problems with? regular-expressions.info is a good place to start learning regular expressions. Commented Jul 24, 2012 at 11:49
  • 1
    @TimPietzcker he said . or , as decimal point, so I think not :) Commented Jul 24, 2012 at 11:50
  • 1
    And what expressions did you try, and what went wrong? Have you got a demo we can play with? Help us to help you. Commented Jul 24, 2012 at 11:50
  • if you dont need currency symbol use parseFloat: parseFloat("40€") == 40.0 Commented Jul 24, 2012 at 11:52
  • thanks all! ill check out that website. i tried a bunch of expressions but i just got extremely confused.. the expression need to check for the currency symbol also.. Commented Jul 24, 2012 at 12:20

3 Answers 3

6

The regex will look like:

\d+(?:[.,]\d+)?€/g

In Javascript, as a regex object (note that the forward slash needs to be escaped):

/\d+(?:[.,]\d+)?€\/g/

Here's a breakdown of what each part does:

\d+  # one or more digits
(?:    # ... don't capture this group separately
 [.,] # decimal point
 \d+  # one or more digits
)?   # make the group optional
€/g  # fixed string to match

If you want to allow something like .123€/g to be valid as well, you can use:

(?=[.,]|\d)(?:\d+)?(?:[.,]\d+)?€/g

That is, both the groups of digits are optional, but at least one must be present (this uses lookahead, which is a bit more tricky).

Note that this will also match constructions like 'word2€/g'. If you want to prevent this, start the regex with (?<=^|\s) (matches if preceded by a space or the start of the string) and end it with (?=$|\s) (matches if followed by a space or the end of the string).

Full-blown version:

(?<=^|\s)(?=[.,]|\d)(?:\d+)?(?:[.,]\d+)?€/g(?=$|\s)
Sign up to request clarification or add additional context in comments.

3 Comments

great answer, thanks! it works perfectly. how about if i want the matched float numbers to end with EITHER "€/g" or " / g"? tried this but didn't seem to work: /\d+(?:[.,]\d+)?[(€\/g)|( \/ g)]/
@urbffe: [(€\/g)|( \/ g)] is a character class, it will only match one of the provided characters. I.e. it is the same as [()€/g]. I think you want (€\/g| \/ g).
This also matches 001,2€/g
2
\d+([.,]\d+)?€/g

should work, I guess.

3 Comments

You will have to escape / in the regex literal, but by itself the expression looks fine.
As a rule for myself, I never give regexes in literal form like you'd use it in the language. This simplifies a lot, especially for Java (which has no literal strings) or PHP (where you have to use a string and delimiters – craziness). I usually assume that the reader knows enough about the language to adapt the regex itself to the language they're using :-)
I usually assume that the reader knows enough about the language to adapt the regex itself to the language they're using ... I wish it was so :) Nevertheless, I like your attitude... happy coding!
0

Are you really sure you need a regex for this? It might be easier to instead leverage the builtin floating point parsing that is available: take whatever comes before the euro sign, normalize commas to decimals (or vice versa, whatever ends up working) and then try to parse it with the Number function. Note that you would need to check if the conversion worked with the Number.isNaN function.

Another possibility is to just use the parseFloat function. Since it ignores any characters after the numbers then it would parse "40€ as 40.0. However, it might not be what you want since it would also allow things like "40a" and "40b" as well.

2 Comments

thanks for ur answer. i see what you mean but it's not exactly what i'm looking for. i'm searching a large string for float numbers which specifically has the €/g appended.
Even then, instead of doing a complicated regex, you could instead search for things that "kind of look like a number" (but needing only a simpler regex) and then filter over the list with the builtin parsing function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.