Forum: Ruby on Rails How to find particular pattern in string?

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.
8c70506408fddb9740b4de7339055c77?d=identicon&s=25 Prashant Tiwari (tiwaripk)
on 2006-06-08 07:33
Hi,

In my application I want to find out the occurance of substring
"http:\\" in the main string "http:\\www.abc.com"

Here how to find out whether substring "http:\\" is present in my
String?

& if substring "http:\\" is present then I want to delete it from main
string.
How to do this?

PLs help me.

Thanx in advance.
Prash
B780ee0ee1480454a85df58536702f63?d=identicon&s=25 Alder Green (Guest)
on 2006-06-08 08:33
(Received via mailing list)
On 6/8/06, Prashant Tiwari <tiwari_p_k@yahoo.com> wrote:
> Hi,
>
> In my application I want to find out the occurance of substring
> "http:\\" in the main string "http:\\www.abc.com"
>
> Here how to find out whether substring "http:\\" is present in my
> String?
>

s = "http:\\www.abc.com"
regexp = %r{http:\\}
s =~ regexp

> & if substring "http:\\" is present then I want to delete it from main
> string.
> How to do this?

s.gsub(regexp, '')
8c70506408fddb9740b4de7339055c77?d=identicon&s=25 Prashant Tiwari (tiwaripk)
on 2006-06-08 09:05
Hi alder,
Thanx for ur reply.
I tried with your suggestion. But it works only when there is blank
space between "http:\\" & rest of string in my Main String.
ie, it is working only when my mainstring is like :-
"http:\\ www.abc.com"  (note blank space betn "http:\\" and
"www.abc.com" )
& when my string is like "http:\\www.abc.com" (without balnk space) its
not working.
Can you please again tell me with modifying your above solution for my
mainstring without blank space?

Please help me.
Thanx again
pras


Alder Green wrote:
> On 6/8/06, Prashant Tiwari <tiwari_p_k@yahoo.com> wrote:
>> Hi,
>>
>> In my application I want to find out the occurance of substring
>> "http:\\" in the main string "http:\\www.abc.com"
>>
>> Here how to find out whether substring "http:\\" is present in my
>> String?
>>
>
> s = "http:\\www.abc.com"
> regexp = %r{http:\\}
> s =~ regexp
>
>> & if substring "http:\\" is present then I want to delete it from main
>> string.
>> How to do this?
>
> s.gsub(regexp, '')
B780ee0ee1480454a85df58536702f63?d=identicon&s=25 Alder Green (Guest)
on 2006-06-08 09:32
(Received via mailing list)
On 6/8/06, Prashant Tiwari <tiwari_p_k@yahoo.com> wrote:
> mainstring without blank space?
The above solution should and does work, regardless of the character
following the substring. I tested it with the strings you provided,
and it did work. Check if you made an error trying it, perhaps using
%r{http:\\ } for the regexp.
9d7d8ef2179661d6b30e180fa588cd45?d=identicon&s=25 Calle Dybedahl (Guest)
on 2006-06-08 10:16
(Received via mailing list)
>>>>> "Prashant" == Prashant Tiwari <tiwari_p_k@yahoo.com> writes:

> Hi,
> In my application I want to find out the occurance of substring
> "http:\\" in the main string "http:\\www.abc.com"

Methods for that live in the aptly named Ruby class String. In this
case, it sounds like you want the method String#index.
--
		     Calle Dybedahl <calle@cyberpomo.com>
		 http://www.livejournal.com/users/cdybedahl/
      "It's not much of a silver lining, but I'll take what I can get."
			  -- yasminm, on LiveJournal
05faf3e7b9ac65d5090ea404c27f8f26?d=identicon&s=25 Chen Ge (chenge)
on 2006-06-08 11:23
(Received via mailing list)
Calle Dybedahl <rails@...> writes:

>
> >>>>> "Prashant" == Prashant Tiwari <tiwari_p_k@...> writes:
>
> > Hi,
> > In my application I want to find out the occurance of substring
> > "http:\\" in the main string "http:\\www.abc.com"
>
> Methods for that live in the aptly named Ruby class String. In this
> case, it sounds like you want the method String#index.


s='http://www.abc.com'
pat='http://'
n=s.index(pat)
if n!=nil
	s.sub!(pat, '')
end

This is simpler than regex.
This topic is locked and can not be replied to.