Forum: Ruby writing regular expression,

Cc704ce316e72af824ccd38cfa53104b?d=identicon&s=25 Raja Gopalan (arc)
on 2013-10-22 08:12
hi,

I need to replace the string like

A1:BA23 => A1:B{value}

How would I write the regular expression for this?(number after : has to
be replace with{value}
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-10-22 08:37
Without explaining the rules or giving more than one example, it's
impossible to be precise enough. Is it always a colon, one character,
then to the end of the string? Is it always ":B#{value}", or could it be
":A#{value}"...
Cc704ce316e72af824ccd38cfa53104b?d=identicon&s=25 Raja Gopalan (arc)
on 2013-10-22 08:41
Oh sorry, It could be ":A#{value}" also
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-10-22 10:49
You should use http://www.rubular.com/ to experiment on this kind of
thing.

Here's a possibility:
'A1:BA23'.sub( /(?<=:\w)\w+/, 'moose' )
=> "A1:Bmoose"
Cc704ce316e72af824ccd38cfa53104b?d=identicon&s=25 Raja Gopalan (arc)
on 2013-10-22 11:09
while I am executing your code, I am getting the error

puts 'A1:BA23'.sub( /(?<=:\w)\w+/, 'moose' )

Error:
undefined (?...) sequence: /(?<=:\w)\w+/

and by the way, here you replaced A as well, But My condition was, only
replace the integer value comes after ":".
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-10-22 12:56
No, you clearly stated in your question that you replace "A23", not only
the digits. If that's all you want to do, then just use this:

'A1:BA23'.sub( /\d+$/, 'numbers' )
Cc704ce316e72af824ccd38cfa53104b?d=identicon&s=25 Raja Gopalan (arc)
on 2013-10-22 13:09
hi

<<you clearly stated in your question that you replace "A23",>>
Yes yes I am sorry.

<<'A1:BA23'.sub( /\d+$/, 'numbers' )>>
yes it's great, it's working. Thank you.

But my question continues here,

What if the string is 'A1:BA23fd232' ? it will replace the last digits,
but I want the one which immediate next to the ":", Is that possible to
replace like that?
Cc704ce316e72af824ccd38cfa53104b?d=identicon&s=25 Raja Gopalan (arc)
on 2013-10-22 13:13
But my question continues here,

What if the string is 'A1:BA23fd232' ? it will replace the last digits,
but I want the one which immediate next to the ":", Is that possible to
replace like that?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2013-10-22 13:15
(Received via mailing list)
On Tue, Oct 22, 2013 at 1:13 PM, Raja gopalan <lists@ruby-forum.com>
wrote:

> What if the string is 'A1:BA23fd232' ? it will replace the last digits,
> but I want the one which immediate next to the ":", Is that possible to
> replace like that?

What other formats are there?  To make everybody's life easier please
provide a list of all possible inputs and desired outputs.

Cheers

robert
Cc704ce316e72af824ccd38cfa53104b?d=identicon&s=25 Raja Gopalan (arc)
on 2013-10-22 13:21
Hi Robert Klemme
Actually I am not testing anyone here, My requirement is like that,
I have big string like 'A1:B2 hi how are B3" available with me, So I
need to replace the second one B{something}, that's I asked him.
Actually I got the result after he replied, but the problem was I
noticed some other string like I have given.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2013-10-22 13:50
(Received via mailing list)
On Tue, Oct 22, 2013 at 1:21 PM, Raja gopalan <lists@ruby-forum.com>
wrote:
> Hi Robert Klemme
> Actually I am not testing anyone here,

I did not assume that. What makes you think I did?

> My requirement is like that,
> I have big string like 'A1:B2 hi how are B3" available with me, So I
> need to replace the second one B{something}, that's I asked him.

Yes, and our requirement is to know the different variants of text you
want processed in order to be able to come up with a working solution.
If you do not provide that or alternatively describe how your texts
will look like you will probably not get something useful in return.
It's as easy as that.

Cheers

robert
Cc704ce316e72af824ccd38cfa53104b?d=identicon&s=25 Raja Gopalan (arc)
on 2013-10-22 13:55
Ok I will ask you in more clear way,

for an example,

string 1 : "A1:B12somestringB23"
string 2 : "somB12estring A1:B23"

now output of the above two strings should be,

"A1:B#{value}somestringB23"
"somB12estring A1:B#{value}"

Hope I made my question clear. For that I need a regular expression.
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-10-22 13:58
irb(main):003:0> s1.gsub /(:[a-z]+)\d+/i, '\1moose'
=> "A1:BmoosesomestringB23"
irb(main):004:0> s2.gsub /(:[a-z]+)\d+/i, '\1moose'
=> "somB12estring A1:Bmoose"
Cc704ce316e72af824ccd38cfa53104b?d=identicon&s=25 Raja Gopalan (arc)
on 2013-10-22 14:02
hi Joel Pearson

That's great, it's working. thank you.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2013-10-22 14:22
(Received via mailing list)
irb(main):003:0> ["A1:B12somestringB23", "somB12estring A1:B23"].each do
|s|
irb(main):004:1* printf "%s -> %s\n", s, s.sub(/(?<=:B)\d+/, "<VALUE>")
irb(main):005:1> end
A1:B12somestringB23 -> A1:B<VALUE>somestringB23
somB12estring A1:B23 -> somB12estring A1:B<VALUE>
=> ["A1:B12somestringB23", "somB12estring A1:B23"]
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-10-22 14:30
I tried that, but it broke when I tried to account for this: "A1:BA23".
Since I couldn't use a variable number of characters in a look-behind, I
went for remembering the capture group instead.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2013-10-22 14:34
(Received via mailing list)
On Tue, Oct 22, 2013 at 2:30 PM, Joel Pearson <lists@ruby-forum.com>
wrote:
> I tried that, but it broke when I tried to account for this: "A1:BA23".

That was not in the supposedly complete list. ;-)

> Since I couldn't use a variable number of characters in a look-behind, I
> went for remembering the capture group instead.

As always, there plenty of ways - especially with regexp.

Cheers

robert
Cc704ce316e72af824ccd38cfa53104b?d=identicon&s=25 Raja Gopalan (arc)
on 2013-10-22 14:42
Hi, it throws the following error to me,


irb(main):005:0> ["A1:B12somestringB23", "somB12estring A1:B23"].each do
|s|
irb(main):006:1* printf "%s -> %s\n", s, s.sub(/(?<=:B)\d+/, "{lim}")
irb(main):007:1> end
SyntaxError: compile error
(irb):6: undefined (?...) sequence: /(?<=:B)\d+/
        from (irb):7
        from ♥:0
irb(main):008:0>
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-10-22 14:57
Sounds like you're using an obsolete version of Ruby. Which version are
you using?
Cc704ce316e72af824ccd38cfa53104b?d=identicon&s=25 Raja Gopalan (arc)
on 2013-10-22 15:21
1.8.7
Cc704ce316e72af824ccd38cfa53104b?d=identicon&s=25 Raja Gopalan (arc)
on 2013-10-24 15:44
Joel Pearson wrote in post #1125187:
> irb(main):003:0> s1.gsub /(:[a-z]+)\d+/i, '\1moose'
> => "A1:BmoosesomestringB23"
> irb(main):004:0> s2.gsub /(:[a-z]+)\d+/i, '\1moose'
> => "somB12estring A1:Bmoose"

hi Joel Pearson,

I have some doubts from the above regular expression, I don't understand
the meaning of /i and \1. why have you added that?
8e44b211865cd5915f2f883fd960b56d?d=identicon&s=25 Xeno Campanoli (Guest)
on 2013-10-24 15:52
(Received via mailing list)
On 10/24/2013 06:44 AM, Raja gopalan wrote:
> Joel Pearson wrote in post #1125187:
>> irb(main):003:0> s1.gsub /(:[a-z]+)\d+/i, '\1moose'
>> => "A1:BmoosesomestringB23"
>> irb(main):004:0> s2.gsub /(:[a-z]+)\d+/i, '\1moose'
>> => "somB12estring A1:Bmoose"
> hi Joel Pearson,
>
> I have some doubts from the above regular expression, I don't understand
> the meaning of /i and \1. why have you added that?
>

\1 in regular expressions traditionally is used to reference the string
captured from within parentheses of a pattern.  In Your case that is
whatever was gotten by
(:[a-z]+).  This is also often referenced by $1, though only \1 works in
older versions of regex, like that in sed.  I found an example of this
on page 75 of Pickaxe second edition 1.8.
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-10-24 15:53
Test it on irb or read the info on Regexp on www.rubular.com
/i makes the match case-insensitive, so I don't have to use [a-zA-Z]
\1 refers to the last successful match, which in that case was ":B"
8e44b211865cd5915f2f883fd960b56d?d=identicon&s=25 Xeno Campanoli (Guest)
on 2013-10-24 15:58
(Received via mailing list)
On 10/24/2013 06:44 AM, Raja gopalan wrote:
> Joel Pearson wrote in post #1125187:
>> irb(main):003:0> s1.gsub /(:[a-z]+)\d+/i, '\1moose'
>> => "A1:BmoosesomestringB23"
>> irb(main):004:0> s2.gsub /(:[a-z]+)\d+/i, '\1moose'
>> => "somB12estring A1:Bmoose"
> hi Joel Pearson,
>
> I have some doubts from the above regular expression, I don't understand
> the meaning of /i and \1. why have you added that?
>

Looks like the options are described on page 324 of same Pickaxe 2nd
edition:  "Regular Expression Options":

i Case insensitive

o Substitute once

m multiline mode

x extended mode

xc
Cc704ce316e72af824ccd38cfa53104b?d=identicon&s=25 Raja Gopalan (arc)
on 2013-10-24 15:58
hi  Xeno Campanoli, Joel Pearson

I understood.

Thank you
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2013-10-24 16:28
(Received via mailing list)
On Thu, Oct 24, 2013 at 3:52 PM, Xeno Campanoli
<xeno.campanoli@gmail.com> wrote:
>> hi Joel Pearson,
>>
>> I have some doubts from the above regular expression, I don't understand
>> the meaning of /i and \1. why have you added that?
>
> \1 in regular expressions traditionally is used to reference the string
> captured from within parentheses of a pattern.  In Your case that is
> whatever was gotten by
> (:[a-z]+).  This is also often referenced by $1, though only \1 works in
> older versions of regex, like that in sed.  I found an example of this on
> page 75 of Pickaxe second edition 1.8.

Where does $1 work in Ruby?  I can only think of the block form:

irb(main):001:0> "foo".gsub /f(o+)/, '<$1>'
=> "<$1>"
irb(main):002:0> "foo".gsub(/f(o+)/) {"<#$1>"}
=> "<oo>"

It does not work with the replacement string form. Or did that change in
Ruby 2?

Kind regards

robert
15000f55138ae94b0f362ed7c625461a?d=identicon&s=25 unknown (Guest)
on 2013-10-25 16:12
(Received via mailing list)
Am 24.10.2013 15:57, schrieb Xeno Campanoli:
>> I have some doubts from the above regular expression, I don't understand
>> the meaning of /i and \1. why have you added that?
>
> Looks like the options are described on page 324 of same Pickaxe 2nd
> edition:  "Regular Expression Options":

These can all be found in the official documentation, too:

  http://www.ruby-doc.org/core-2.0.0/Regexp.html#label-Options

Regards,
Marcus
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.