Hi, new to the forum, not new to Ruby but very new to Regular Expression and they are killing me... I have strings that look like this: "--- this is a string ---" = "--- this is a string ---"; " this is a string " = " this is a string "; "###this is a string" = "###this is a string"; I need to just get both string on each side to the equal sign, not including the equal sign. I need to get all spaces or characters inside the quotes but not including the quotes and not the semi-colon. I need to compare the two strings. So... this is a string this is a string I tried using string.scan/regex/ which I like because it returns an array. Again Fail. I've tried all kinds of RegEx...I simply don't know what I'm doing and I very frustrated. I have been using http://rubular.com/ to experiment but I'm really not having success... My most successful attempt, [^"=;] gets pretty much what I need but I can't not select the space around the =. I've looked at documentation, used Rubular and have tried a lot of different things. Can someone please help me? Thanks Brad
on 2012-10-06 01:11
on 2012-10-06 01:22
Am 06.10.2012 01:11, schrieb Brad Askins: > the quotes but not including the quotes and not the semi-colon. I need > > My most successful attempt, [^"=;] gets pretty much what I need but I > can't not select the space around the =. 1.9.3-p194 :001 > text = '"--- this is a string ---" = "--- this is a string ---";' => "\"--- this is a string ---\" = \"--- this is a string ---\";" 1.9.3-p194 :002 > text.scan(/"(.*?)" = "(.*?)"/).flatten => ["--- this is a string ---", "--- this is a string ---"]
on 2012-10-06 12:13
On Sat, Oct 6, 2012 at 1:21 AM, <sto.mar@web.de> wrote: > 1.9.3-p194 :001 > text = '"--- this is a string ---" = "--- this is a string > ---";' > => "\"--- this is a string ---\" = \"--- this is a string ---\";" > 1.9.3-p194 :002 > text.scan(/"(.*?)" = "(.*?)"/).flatten > => ["--- this is a string ---", "--- this is a string ---"] I'd rather use an approach which explicitly includes the "=" sign, for example irb(main):026:0> text = '"--- this is a string ---" = "--- this is a string ---";' => "\"--- this is a string ---\" = \"--- this is a string ---\";" irb(main):027:0> if %r{"([^"]*)"\s*=\s*"([^"]*)"} =~ text then p $1, $2 end "--- this is a string ---" "--- this is a string ---" => ["--- this is a string ---", "--- this is a string ---"] If one wants to allow escaped quotes inside the string things get a bit more tricky but that's doable as well: irb(main):028:0> text = '"--- this is an escaped quote: \"" = "--- this is a string ---";' => "\"--- this is an escaped quote: \\\"\" = \"--- this is a string ---\";" irb(main):029:0> if %r{"((?:\\.|[^"\\])*)"\s*=\s*"((?:\\.|[^"\\])*)"} =~ text then p $1, $2 end "--- this is an escaped quote: \\\"" "--- this is a string ---" => ["--- this is an escaped quote: \\\"", "--- this is a string ---"] Of course, you could also include the trailing ";" in the match to be even more restrictive. irb(main):030:0> if %r{"((?:\\.|[^"\\])*)"\s*=\s*"((?:\\.|[^"\\])*)"\s*;} =~ text then p $1, $2 end "--- this is an escaped quote: \\\"" "--- this is a string ---" => ["--- this is an escaped quote: \\\"", "--- this is a string ---"] Kind regards robert
on 2012-10-06 12:25
Am 06.10.2012 12:12, schrieb Robert Klemme: > On Sat, Oct 6, 2012 at 1:21 AM, <sto.mar@web.de> wrote: > >> 1.9.3-p194 :001 > text = '"--- this is a string ---" = "--- this is a string >> ---";' >> => "\"--- this is a string ---\" = \"--- this is a string ---\";" >> 1.9.3-p194 :002 > text.scan(/"(.*?)" = "(.*?)"/).flatten >> => ["--- this is a string ---", "--- this is a string ---"] > > I'd rather use an approach which explicitly includes the "=" sign ??? It does.
on 2012-10-06 13:38
On Sat, Oct 6, 2012 at 12:25 PM, <sto.mar@web.de> wrote: >> >> I'd rather use an approach which explicitly includes the "=" sign > > ??? > > It does. Right you are. I let myself irritate by your usage of String#scan. Sorry for that. I do not think this is a case for #scan though since according to the OP's description the String only contains one "assignment". So it's either "match" or "don't match". Kind regards robert
on 2012-10-06 16:33
text.scan(/"(.*?)" = "(.*?)"/).flatten That worked awesome!! So I was getting pretty close. I see what I was doing wrong now that I see the solution. That's for your help. I need more practice on RegEx. Thanks again. Brad
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.