Kamil
Kamil

Reputation: 165

Exclude a single last word from string using capture groups

I'm writing an integer parser in Javascript that takes in verbal descriptions of numbers up to a million. At the moment, I need to get rid of (space)thousand in a string like two hundred forty-two thousand, most preferably using a capture group containing the two hundred forty-two. My problem is, the thousand may or may not be there, and using ? in this case stops my regex from working as intended. Here's what I've got so far:

Considering the string two hundred forty-two thousand

What I need is an expression that puts the whole string in a capture group, except the last, specified word whether it exists or not in the string.

I've been searching a lot for a solution to this, but I couldn't find anywhere a case in which the excluded word is optional in the string

Upvotes: 1

Views: 65

Answers (2)

1983
1983

Reputation: 5963

var re = /^(.*?)( thousand)?$/;
console.log("two hundred forty-two thousand".match(re));
console.log("two hundred forty-two".match(re));
  1. Anchor the match at the beginning and the end of the string.
  2. Use .*? to match lazily (i.e. non-greedily) up to an optional thousand.

Upvotes: 1

Federico Piazza
Federico Piazza

Reputation: 30985

You can create a non capturing group to match the thousand pattern and discard it if present, if it is not there the alternation will match until the end of line.

I can come up with this regex:

(.+?)(?: thousand|$)

Working demo

Regular expression visualization

enter image description here

Upvotes: 1

Related Questions