0

I know, or at least I think I know, what this does (string.split(/\?|\.|!/).size); splits the string at every ending punctuation into an array and then gets the size of the array.

The part I am confused with is (/\?|\.|!/).

Thank you for your explanation.

1
  • 1
    Escaped question mark or escaped dot or not escaped exclamation mark. Any of those.
    – Ivan Danci
    Jul 21, 2015 at 16:38

5 Answers 5

3

Regular expressions are surrounded by slashes / /

The backslash before the question mark and dot means use those characters literally (don't interpret them as special instructions)

The vertical pipes are "or"

So you have / then question mark \? then "or" | then period \. then "or" | then exclamation point ! then / to end the expression.

/\?|\.|!/

2

It's a Regular Expression. That particular one matches any '?', '.' or '!' in the target string.

You can learn more about them here: http://regexr.com/

2

A regular expression splitting on the char "a" would look like this: /a/. A regular expression splitting on "a" or "b" is like this: /a|b/. So splitting on "?", "!" and "." would look like /?|!|./ - but it does not. Unfortunately, "?", and "." have special meaning in regexps which we do not want in this case, so they must be escaped, using "\".

A way to avoid this is to use Regexp.union("?","!",".") which results in /\?|!|\./

0
1
(/\?|\.|!/)

Working outside in:
The parentheses () captures everything enclosed.
The // tell Ruby you're using a Regular Expression.
\? Matches any ?
\. Matches any .
!   Matches any !
The preceding \ tells Ruby we want to find these specific characters in the string, rather than using them as special characters.

Special characters (that need to be escaped to be matched) are:

. | ( ) [ ] { } + \ ^ $ * ?. 

There is a nice guide to Ruby RegEx at:
http://rubular.com/ & http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm

4
  • The regular expression does not contain parentheses and it's \?, not /?
    – Stefan
    Jul 21, 2015 at 16:46
  • Thanks, edited. As for the parentheses: OP expressed confusion in the whole expression so I didn't leave anything out.
    – nextstep
    Jul 21, 2015 at 16:52
  • 2
    Yes, but the regular expression is /\?|\.|!/, the parentheses belong to the split method. BTW, \? matches a single ?, not one or more.
    – Stefan
    Jul 21, 2015 at 16:59
  • Confirmed & edited, thanks. Rubulars matched result box can be misleading.
    – nextstep
    Jul 21, 2015 at 17:12
1

For SO answers that involve regular expressions, I often use the "extended" mode, which makes them self-documenting. This one would be:

r = /
    \? # match a question mark
    |  # or
    \. # match a period
    |  # or
    !  # match an explamation mark
    /x # extended mode

str = "Out, damn'd spot! out, I say!—One; two: why, then 'tis time to " +
      "do't.—Hell is murky.—Fie, my lord, fie, a soldier, and afeard?" 

str.split(r)
  #=> ["Out, damn'd spot",
  #    " out, I say",
  #    "—One; two: why, then 'tis time to do't",
  #    "—Hell is murky",
  #    "—Fie, my lord, fie, a soldier, and afeard"] 
str.split(r).size #=> 5

@steenslag mentioned Regexp::union. You could also use Regexp::new to write (with single quotes):

r = Regexp.new('\?|\.|!')
  #=> /\?|\.|!/

but it really doesn't buy you anything here. You might find it useful in other situations, however.

3
  • Regexp::union handles the correct escaping, that is something.
    – Stefan
    Jul 21, 2015 at 17:38
  • 1
    @Stefan, I meant Regexp::new doesn't really help here. union is useful. I edited to clarify. Jul 21, 2015 at 17:43
  • Oh, of course! I thought "it" would refer to Regexp::union :-)
    – Stefan
    Jul 21, 2015 at 18:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.