Forum: Ruby regex ignore whitespace in target string

Be2b7277cff8516117cbd7a33df7b03a?d=identicon&s=25 Dave Castellano (dcastellano1)
on 2014-02-02 19:34
Hi,

Does anyone know how to ignore whitespace in the target string (Not in
the expression itself, which is what I believe /x does)?

eg  question.match(/\{(male|female),[0-99][0-99]\%\}/)

# "{male/female,50%}"  vs # "{male/female,  50%}"

Thanks,
Dave
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2014-02-02 20:16
Just remove all the whitespace before you match.
question.gsub(/\s+/, '').match ...
2ffac40f8a985a2b2749244b8a1c4161?d=identicon&s=25 Mike Stok (Guest)
on 2014-02-02 20:31
(Received via mailing list)
On Feb 2, 2014, at 1:34 PM, Dave Castellano <lists@ruby-forum.com>
wrote:

> Dave
>
> --
> Posted via http://www.ruby-forum.com/.

You can use \s* to match 0 or more "whitespace" characters, for example

 /\{                       # opening {
  ((?:fe)?male)            # male or female stored into $1
  ,                        # a comma
  \s*                      # zero or more spaces
  0*?(100|[1-9]\d|\d)%     # soak up leading 0s and then have a valid
percentage (0 - 100) -> $2
  %                        # then an %
  \}                       # then a closing }
 /x

might do what you want. You can play with it at
http://rubular.com/r/ASK9yMubOH to see if it does

Hope this helps,

Mike

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
2ffac40f8a985a2b2749244b8a1c4161?d=identicon&s=25 Mike Stok (Guest)
on 2014-02-02 20:47
(Received via mailing list)
On Feb 2, 2014, at 2:30 PM, Mike Stok <mike@stok.ca> wrote:

>>
>  ,                        # a comma
>  \s*                      # zero or more spaces
>  0*?(100|[1-9]\d|\d)%     # soak up leading 0s and then have a valid percentage
(0 - 100) -> $2

There shouldn't be a % in the line above *and* the line below, I made a
mistake while adding comments!

> --
>
--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2014-02-02 23:03
(Received via mailing list)
On Sun, Feb 2, 2014 at 7:34 PM, Dave Castellano <lists@ruby-forum.com>
wrote:
> Hi,
>
> Does anyone know how to ignore whitespace in the target string (Not in
> the expression itself, which is what I believe /x does)?
>
> eg  question.match(/\{(male|female),[0-99][0-99]\%\}/)

You want "\/" rather than "|" between "male" and "female" to match the
given strings.

> # "{male/female,50%}"  vs # "{male/female,  50%}"

As Mike said, you can use \s* to match arbitrary sequences of
whitespace (including the empty one).

irb(main):005:0>
"{male/female,50%}".match(/\{(male\/female),\s*[0-99][0-99]\%\}/)
=> #<MatchData "{male/female,50%}" 1:"male/female">
irb(main):006:0> "{male/female,
50%}".match(/\{(male\/female),\s*[0-99][0-99]\%\}/)
=> #<MatchData "{male/female, 50%}" 1:"male/female">

I just notice you have a duplicate 9 in the character class.  And you
do not need to escape %.  You probably rather want:

/\{(male\/female),\s*\d{1,2}%\}/

Kind regards

robert
Be2b7277cff8516117cbd7a33df7b03a?d=identicon&s=25 Dave Castellano (dcastellano1)
on 2014-02-02 23:06
Thanks!
- very helpful.

Dave
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.