but it includes the markers in the capture i.e. it gives ((Great))
when
i just want Great.
The fact that the parens are delimiters obscures the regexp a bit.
Assuming “))” ends the text to extract unconditionally you can simply
use .*? like this
irb(main):001:0> "ruby is ((Great))".match(/ \(\( (.*?) \)\) /mx)[1]
=> "Great"
ahh just noticed the inclusion of the ? to make it restricted. I
understand your use of groups there with [1] but not to sure of that
syntax. I was thinking of using string.slice or string.scan to return
the text but not sure how i would do so with groups. How would i go
about doing this???
I just wondered, if I had multiple marked sections in a string how
would
i capture all of them?
So if my sentance was
“a bannana is a type of ((fruit)) and a dog is a type of ((animal))”
how
could I store fruit and animal for later use via regex?
irb(main):004:0> str = “a bannana is a type of ((fruit)) and a dog is
a type of ((animal))”
=> “a bannana is a type of ((fruit)) and a dog is a type of ((animal))”
ahhh thats great. I like the readability one. ill be using that a lot
from now on and ill use match instead of scan and slice for this
particular problem. Thanks Xavier.
great, closer to solving my problem. Though i realised that this regex
wouldnt work if the marked text was split by a newline so i went away
and modified it so that if it were split it would still be picked up. I
did it with this
({2}(?s)(.*?)(?s)){2}
Im wondeirng if theres a neater way of sayinig “ignore any newlines that
split the marked text up”
is there an operator that tells it to ignore newlines and is the above
robust?
With /x literal whitespace in the regexp are ignored. Since the regexp uses
so many backslashes that gives some air for readability.
I know I’m in the minority, but I’ll just mention that I find most
regexes that make use of /x very hard to read. The reason is that I’ve
trained my brain how to read a pattern, so if I encounter this:
/ string (.*) another ? string /x
it’s a considerable effort to “not see” the spaces. I think it’s
better to stick to the basic pattern language, which after all is the
only set of rules that we all learn and all share.
I would recommend saving /x for cases where you want to break a regex
out into multiple lines and include comments:
re = /
( # opening paren
\d{3} # area code
) # closing paren
etc. (Not a great example of an obscure pattern that’s made more clear
by /x but you get the idea.)
David
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.