6 Replies - 732 Views - Last Post: 29 December 2013 - 05:33 PM Rate Topic: -----

#1 heaphyg   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

regex question

Posted 29 December 2013 - 02:04 PM

The code below takes a number and places commas into it. ex. 1000 => 1,000. I am not very clear on how the regex works however. I understand the meaning of the code leading up to and following the regex. My current interpretation of the regex is that 3 digits into the reversed number a comma will be inserted even if those 3 digits are followed by a single digit. I am a bit confused. How does this regex deal with numbers greater than 4 digits in length? In addition what is with the double backslash before the 1? Thanks.

number.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse



Is This A Good Question/Topic? 0
  • +

Replies To: regex question

#2 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: regex question

Posted 29 December 2013 - 02:21 PM

'\1' would normally be a reference to the first capture group (\d{3}) but the gsub method requires it to be double-escaped (two backslashes).

(?=\d) is a positive lookahead, requiring that the three captured digits need to be following by another digit, which is not captured.

So '\\1,' will insert the 3 captured digits followed by a comma. The other digit, or digits, will follow this because they weren't captured.

This post has been edited by andrewsw: 29 December 2013 - 02:22 PM

Was This Post Helpful? 2
  • +
  • -

#3 heaphyg   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: regex question

Posted 29 December 2013 - 04:31 PM

Thank you very much for the helpful reply. so if I'm correct this regex looks for ALL 3 digit sequences followed by a number and inserts a comma. Just for edification if we only wanted to insert the comma only once even if we had a number like 23000000 (resulting in 23000,000) could we simply just replace the (?= \d) with (? = \d+)?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: regex question

Posted 29 December 2013 - 04:37 PM

Probably, did you try it? Doesn't it do this already?
Was This Post Helpful? 0
  • +
  • -

#5 heaphyg   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: regex question

Posted 29 December 2013 - 04:37 PM

Also the functioning code below uses the gsub method to insert dashes into a social security number. However, this code only has single slashes referencing our capture groups. I am still a bit unclear about why the previous example necessitates double slashes to reference our capture groups. Thanks

def format_ssns(string)
  string.gsub /\b(\d{3})\D?(\d{2})\D?(\d{4})\b/, '\1-\2-\3
end


Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: regex question

Posted 29 December 2013 - 04:53 PM

I am not very familiar with Ruby, but in the first code it might work with a single backslash. If you double quote it though (as a string) you might need the double-backslash.

Experiment a little.

Once you've experimented I wouldn't worry too much. Different languages, and contexts, require different numbers of backslashes. I've had to use as many as five (or six) in the past :helpsmilie:



In a string \ is the escape character, so \d would just be escaping the d, whereas you need to escape the backslash \\d so that it is still interpreted as \d when passed to the regex-interpreter. Make sense? ;)

This post has been edited by andrewsw: 29 December 2013 - 05:00 PM

Was This Post Helpful? 1
  • +
  • -

#7 heaphyg   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: regex question

Posted 29 December 2013 - 05:33 PM

Yes. Thank you very much for your time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1