Forum: Ruby writing regular expression,

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
41ffd3f2fee19dbac758664759b0cdfc?d=identicon&s=25 Raja G. (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}
9627ec27e4fb3a18be02133b4ddaa901?d=identicon&s=25 Joel P. (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}"...
41ffd3f2fee19dbac758664759b0cdfc?d=identicon&s=25 Raja G. (arc)
on 2013-10-22 08:41
Oh sorry, It could be ":A#{value}" also
9627ec27e4fb3a18be02133b4ddaa901?d=identicon&s=25 Joel P. (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"
41ffd3f2fee19dbac758664759b0cdfc?d=identicon&s=25 Raja G. (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 ":".
9627ec27e4fb3a18be02133b4ddaa901?d=identicon&s=25 Joel P. (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' )
41ffd3f2fee19dbac758664759b0cdfc?d=identicon&s=25 Raja G. (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?
41ffd3f2fee19dbac758664759b0cdfc?d=identicon&s=25 Raja G. (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?
13d0171c6ee97074d61f182a6e3a9f4e?d=identicon&s=25 Robert K. (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
41ffd3f2fee19dbac758664759b0cdfc?d=identicon&s=25 Raja G. (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.
13d0171c6ee97074d61f182a6e3a9f4e?d=identicon&s=25 Robert K. (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
41ffd3f2fee19dbac758664759b0cdfc?d=identicon&s=25 Raja G. (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.
9627ec27e4fb3a18be02133b4ddaa901?d=identicon&s=25 Joel P. (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"
41ffd3f2fee19dbac758664759b0cdfc?d=identicon&s=25 Raja G. (arc)
on 2013-10-22 14:02
hi Joel Pearson

That's great, it's working. thank you.
13d0171c6ee97074d61f182a6e3a9f4e?d=identicon&s=25 Robert K. (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"]
9627ec27e4fb3a18be02133b4ddaa901?d=identicon&s=25 Joel P. (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.
13d0171c6ee97074d61f182a6e3a9f4e?d=identicon&s=25 Robert K. (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
41ffd3f2fee19dbac758664759b0cdfc?d=identicon&s=25 Raja G. (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>
9627ec27e4fb3a18be02133b4ddaa901?d=identicon&s=25 Joel P. (virtuoso)
on 2013-10-22 14:57
Sounds like you're using an obsolete version of Ruby. Which version are
you using?
41ffd3f2fee19dbac758664759b0cdfc?d=identicon&s=25 Raja G. (arc)
on 2013-10-22 15:21
1.8.7
41ffd3f2fee19dbac758664759b0cdfc?d=identicon&s=25 Raja G. (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?
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.
9627ec27e4fb3a18be02133b4ddaa901?d=identicon&s=25 Joel P. (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"
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
41ffd3f2fee19dbac758664759b0cdfc?d=identicon&s=25 Raja G. (arc)
on 2013-10-24 15:58
hi  Xeno Campanoli, Joel Pearson

I understood.

Thank you
13d0171c6ee97074d61f182a6e3a9f4e?d=identicon&s=25 Robert K. (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
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
This topic is locked and can not be replied to.