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.
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 —”]
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 —”]
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.