Writing regular expression,

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}

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}”…

Oh sorry, It could be “:A#{value}” also

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”

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 “:”.

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’ )

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?

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?

Hi Robert K.
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.

On Tue, Oct 22, 2013 at 1:13 PM, Raja gopalan [email protected]
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

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.

On Tue, Oct 22, 2013 at 1:21 PM, Raja gopalan [email protected]
wrote:

Hi Robert K.
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

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”

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+/, “”)
irb(main):005:1> end
A1:B12somestringB23 -> A1:BsomestringB23
somB12estring A1:B23 -> somB12estring A1:B
=> [“A1:B12somestringB23”, “somB12estring A1:B23”]

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.

hi Joel P.

That’s great, it’s working. thank you.

On Tue, Oct 22, 2013 at 2:30 PM, Joel P. [email protected]
wrote:

I tried that, but it broke when I tried to account for this: “A1:BA23”.

That was not in the supposedly complete list. :wink:

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

Sounds like you’re using an obsolete version of Ruby. Which version are
you using?

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 :heart::0
irb(main):008:0>

1.8.7

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs