# How to find particular pattern in string?

**URL:** <https://www.ruby-forum.com/t/how-to-find-particular-pattern-in-string/62800>\
**Category:** Rails\
**Created:** [June 8, 2006, 7:33am UTC](https://www.ruby-forum.com/t/how-to-find-particular-pattern-in-string/62800 "2006-06-08T07:33:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [June 8, 2006, 7:33am UTC](https://www.ruby-forum.com/t/how-to-find-particular-pattern-in-string/62800/1 "2006-06-08T07:33:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [June 8, 2006, 8:33am UTC](https://www.ruby-forum.com/t/how-to-find-particular-pattern-in-string/62800/2 "2006-06-08T08:33:43Z")

</div>

On 6/8/06, Prashant T. [removed\_email\_address@domain.invalid](mailto: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, ‘’)

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [June 8, 2006, 9:05am UTC](https://www.ruby-forum.com/t/how-to-find-particular-pattern-in-string/62800/3 "2006-06-08T09:05:46Z")

</div>

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](http://www.abc.com)” (note blank space betn “http:\” and  
“[www.abc.com](http://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](mailto: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, ‘’)

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [June 8, 2006, 9:32am UTC](https://www.ruby-forum.com/t/how-to-find-particular-pattern-in-string/62800/4 "2006-06-08T09:32:32Z")

</div>

On 6/8/06, Prashant T. [removed\_email\_address@domain.invalid](mailto: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.

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [June 8, 2006, 11:23am UTC](https://www.ruby-forum.com/t/how-to-find-particular-pattern-in-string/62800/5 "2006-06-08T11:23:26Z")

</div>

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](http://www.abc.com)’  
pat=‘http://’  
n=s.index(pat)  
if n!=nil  
s.sub!(pat, ‘’)  
end

This is simpler than regex.

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [June 8, 2006, 10:16am UTC](https://www.ruby-forum.com/t/how-to-find-particular-pattern-in-string/62800/6 "2006-06-08T10:16:13Z")

</div>

> > > > > “Prashant” == Prashant T. [removed\_email\_address@domain.invalid](mailto: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

```
