Jannik
Jannik

Reputation: 90

Regex: Ignore whitespace before and after a char

I´m using a regular expression to determine if an '€' is wrongly recognized as an 'e' (For example: Te 23,4 means T€ 23,4 or e23,4 is €23,4 and so on). In my case the expression : ^(T|(?i)(Mio)|(Million)(?-i))?( )?[e]( )?\d*$ works well. BUT there is one case in which my expression doesn´t work. If there is whitespace before AND after the 'e' (" e ") the expression does not work.

How can I expand my expression so that this case will not be considered? I think at least there have to be a character before OR after the 'e' but I dont know how to implement that in my expression.

This regex(((.)[e])|([e](.)))works for the whitespace problem, but how can I merge both expressions?

Upvotes: 1

Views: 612

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

In your regex, the first alternation group is optional (? matches 1 or 0 times), as well as the digit matching part (* matches 0 or more chars).

So, the point is to make them obligatory:

(^|T|(?i:Mio|Million))( ?)e( ?)\d+
 ^^                             ^^

See the regex demo. The ^ is moved to the alternation group (enhanced a bit) and \d is quantified with + (1 or more times).

Details

  • (^|T|(?i:Mio|Million)) - one of the alternatives:
    • ^ - start of string
    • T - a T
    • (?i:Mio|Million) - a modifier group matching the 2 alternatives in a case insensitive way: Mio or Million
  • ( ?) - an optional space
  • e - an e
  • ( ?) - an optional space
  • \d+ - 1 or more digits.

Upvotes: 1

Related Questions