# Regexpression: scanning a special charachters //?

**URL:** <https://www.ruby-forum.com/t/regexpression-scanning-a-special-charachters/115992>\
**Category:** Ruby\
**Created:** [October 1, 2007, 2:05pm UTC](https://www.ruby-forum.com/t/regexpression-scanning-a-special-charachters/115992 "2007-10-01T14:05:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [October 1, 2007, 2:05pm UTC](https://www.ruby-forum.com/t/regexpression-scanning-a-special-charachters/115992/1 "2007-10-01T14:05:45Z")

</div>

Hi again,  
I need to parse a line like this here:

```
   myoverlook://thatstrue@blablabla

```

I need at the end just thatstrue. So I wrote for the beginning:

```
  result = overlook.scan(/myoverlook://(.+?)\n/m)

```

Of course this doesn’t work because of the // which are special  
charachters in a regexpr. But how can I tell that they shoudln’t be  
treated as specialcharachters? Is there an escape charachter? I tried it  
so:

```
  result = overlook.scan(/myoverlook:\/\/\(.+?)\n/m)

```

but this throws an error: unmatched ): /myoverlook://(.+?)\n/

What’s the correct synthax for this?

bye

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [October 1, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/regexpression-scanning-a-special-charachters/115992/2 "2007-10-01T14:13:58Z")

</div>

Am Mon, 1 Oct 2007 21:05:06 +0900  
schrieb kazaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> charachters in a regexpr. But how can I tell that they shoudln’t be  
> treated as specialcharachters? Is there an escape charachter? I tried  
> it so:
> 
> ```
> result = overlook.scan(/myoverlook:\/\/\(.+?)\n/m)
> 
> ```
> 
> but this throws an error: unmatched ): /myoverlook://(.+?)\n/
> 
> What’s the correct synthax for this?
> 
> bye

You escaped the ( to, so that your cloasing ) is unmatched…

/myoverlook://(.+?)\n/m ← yours  
/myoverlook://(.+?)\n/m ← more correct (untested)

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [October 1, 2007, 3:13pm UTC](https://www.ruby-forum.com/t/regexpression-scanning-a-special-charachters/115992/3 "2007-10-01T15:13:10Z")

</div>

kazaam wrote:

> Is there an escape charachter? I tried it so:
> 
> ```
> result = overlook.scan(/myoverlook:\/\/\(.+?)\n/m)
> 
> ```

That would be correct if you hadn’t accidentally escaped the (, too.  
A cleaner way would be to use %r{} instead of //, so that / isn’t  
treated as a  
special character and you don’t need to escape it. Like so:  
result = overlook.scan(%r{myoverlook://(.+?)\n}m)

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [October 1, 2007, 3:21pm UTC](https://www.ruby-forum.com/t/regexpression-scanning-a-special-charachters/115992/4 "2007-10-01T15:21:22Z")

</div>

thanks for both your answers. I had thought I had to surround a / with  
two \ to escape it. Now it works 🙂

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [October 1, 2007, 3:20pm UTC](https://www.ruby-forum.com/t/regexpression-scanning-a-special-charachters/115992/5 "2007-10-01T15:20:12Z")

</div>

2007/10/1, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> kazaam wrote:
> 
> > Is there an escape charachter? I tried it so:
> > 
> > ```
> > result = overlook.scan(/myoverlook:\/\/\(.+?)\n/m)
> > 
> > ```
> 
> That would be correct if you hadn’t accidentally escaped the (, too.  
> A cleaner way would be to use %r{} instead of //, so that / isn’t treated as a  
> special character and you don’t need to escape it. Like so:  
> result = overlook.scan(%r{myoverlook://(.+?)\n}m)

I’d also change the RX to a more appropriate one, e.g. any of these

%r{myoverlook://(\w+)@}  
%r{myoverlook://([^@]+}

You can then extract the part you are looking for from group 1.

Depends on the surrounding context which of the RX is better suited.  
I’d probably prefer the fist one since it is more robust.

Kind regards

robert
