1

This is more of a "What regex do I use" rather than a semantics question.

I have the following string :

moneyString = "¥10,100 YEN,€100.00 EU,$100.00 US"

And I need to split it on the comma. However, I don't want the comma in the 10,000 Yen to be separated into two arrays.

Currently, if I do moneyString.split(',')

I get : [¥10, 100 YEN, €100.00 EU, $100.00 US] as the different array values. But I want :

[¥10100 YEN, €100.00 EU, $100.00 US]

Can someone show me how to get this regex correct? I'm sorry, but I am a complete newbie with this stuff.

1
  • 2
    Where does YEN come from? Commented Mar 24, 2014 at 21:41

2 Answers 2

6

You could split on all commas that are NOT preceded by a number, using negative lookbehind.

moneyString = "¥10,100 YEN,€100.00 EU,$100.00 US"

puts moneyString.split(/(?<!\d),/)

# ¥10,100 YEN
# €100.00 EU
# $100.00 US
Sign up to request clarification or add additional context in comments.

Comments

1
moneyString.split(/(?<!\d),/)

The keyword is "negative look-behind".

Comments

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.