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.
Prashant T. (Guest)
on 2006-06-08 09: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
Alder G. (Guest)
on 2006-06-08 10:33
(Received via mailing list)
On 6/8/06, Prashant T. <removed_email_address@domain.invalid> 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, '')
Prashant T. (Guest)
on 2006-06-08 11: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 G. wrote:
> On 6/8/06, Prashant T. <removed_email_address@domain.invalid> 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, '')
Alder G. (Guest)
on 2006-06-08 11:32
(Received via mailing list)
On 6/8/06, Prashant T. <removed_email_address@domain.invalid> 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.
Calle D. (Guest)
on 2006-06-08 12:16
(Received via mailing list)
>>>>> "Prashant" == Prashant T. <removed_email_address@domain.invalid> 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 D. <removed_email_address@domain.invalid>
		 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
Chen G. (Guest)
on 2006-06-08 13:23
(Received via mailing list)
Calle D. <rails@...> writes:

>
> >>>>> "Prashant" == Prashant T. <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.