# Regex help partial code

**URL:** https://rubytalk.org/t/regex-help-partial-code/43748
**Category:** ruby-talk
**Created:** [13 January 2008 21:30 UTC](https://rubytalk.org/t/regex-help-partial-code/43748 "2008-01-13T21:30:02Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Junkone](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@Junkone](https://rubytalk.org/u/Junkone)
#### Post date: [13 January 2008 21:30 UTC](https://rubytalk.org/t/regex-help-partial-code/43748/1 "2008-01-13T21:30:02Z")

</div>

Hello  
I have with great effort started on teh regex implemention and am  
stuck at one point.  
here is my code  
(add)\s+\S+\s+[i|s|f]\s+[pei]?\s+

The string is as follows and the tokens are equavalent to  
1 token=Add  
2 token=any text  
3. token=one of the following i or s or f  
4.token= 0 or 1 of the following characters [p e i]

I am stuck at the 4th token. i thought ? after the token will give  
the 0 or 1 option.  
pl correct me if i made a error

seede

---

<div class="post-metadata">

### Author: ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)
#### Post date: [13 January 2008 22:49 UTC](https://rubytalk.org/t/regex-help-partial-code/43748/2 "2008-01-13T22:49:51Z")

</div>

> Hello  
> I have with great effort started on teh regex implemention and am  
> stuck at one point.  
> here is my code  
> (add)\s+\S+\s+[i|s|f]\s+[pei]?\s+
> 
> The string is as follows and the tokens are equavalent to  
> 1 token=Add

Unless you use the i modifier, ruby regexps are case sensitive. So  
/add/ won't match /Add/, but /add/i will.

> 2 token=any text  
> 3. token=one of the following i or s or f

You want /[isf]/ /[i|s|f]/ is probably equivalent to /[isf|]/

> 4.token= 0 or 1 of the following characters [p e i]

/[pei]?/

> I am stuck at the 4th token. i thought ? after the token will give  
> the 0 or 1 option.

It does.

> pl correct me if i made a error

Show the exact regexp and the string it failed on.

John Carter Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
New Zealand

> **···**
>
> On Mon, 14 Jan 2008, Junkone wrote:

---

<div class="post-metadata">

### Author: ![Junkone](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@Junkone](https://rubytalk.org/u/Junkone)
#### Post date: [13 January 2008 23:45 UTC](https://rubytalk.org/t/regex-help-partial-code/43748/3 "2008-01-13T23:45:04Z")

</div>

irb(main):046:0\> a=Regexp.new('(add)\s+(\S+)\s+([i|s|f])\s+([pei])?\s  
+',true)  
=\> /(add)\s+(\S+)\s+([i|s|f])\s+([pei])?\s+/i  
irb(main):047:0\> a =~("Add comp i pe")  
=\> nil

I dont get match at 4th token

> **···**
>
> On Jan 13, 4:25 pm, Junkone \<junko...@gmail.com\> wrote:
> 
> > Hello  
> > I have with great effort started on teh regex implemention and am  
> > stuck at one point.  
> > here is my code  
> > (add)\s+\S+\s+[i|s|f]\s+[pei]?\s+
> > 
> > The string is as follows and the tokens are equavalent to  
> > 1 token=Add  
> > 2 token=any text  
> > 3. token=one of the following i or s or f  
> > 4.token= 0 or 1 of the following characters [p e i]
> > 
> > I am stuck at the 4th token. i thought ? after the token will give  
> > the 0 or 1 option.  
> > pl correct me if i made a error
> > 
> > seede

---

<div class="post-metadata">

### Author: ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)
#### Post date: [14 January 2008 00:56 UTC](https://rubytalk.org/t/regex-help-partial-code/43748/4 "2008-01-14T00:56:55Z")

</div>

irb  
irb(main):002:0\> /(add)\s+(\S+)\s+([i|s|f])\s+([pei])?\s+/i =~ "Add comp i pe"  
=\> nil  
irb(main):003:0\> /(add)\s+(\S+)\s+([i|s|f])\s+([pei])?/i =~ "Add comp i pe"  
=\> 0

The last \s+ insists on matching one or more whitespace chars.

Here is a general recipe for solving this class of problems....

Set up a simple test like I have above...

Delete elements out of the regex until it matches.

The problem was the last one you deleted.

John Carter Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
New Zealand

> **···**
>
> On Mon, 14 Jan 2008, Junkone wrote:
> 
> > irb(main):046:0\> a=Regexp.new('(add)\s+(\S+)\s+([i|s|f])\s+([pei])?\s  
> > +',true)  
> > =\> /(add)\s+(\S+)\s+([i|s|f])\s+([pei])?\s+/i  
> > irb(main):047:0\> a =~("Add comp i pe")  
> > =\> nil
> > 
> > I dont get match at 4th token

---

<div class="post-metadata">

### Author: ![yermej](https://avatars.discourse-cdn.com/v4/letter/y/b2d939/32.png) [@yermej](https://rubytalk.org/u/yermej)
#### Post date: [14 January 2008 06:25 UTC](https://rubytalk.org/t/regex-help-partial-code/43748/5 "2008-01-14T06:25:11Z")

</div>

There are a few problems here, I think.  
Regexp.new('(add)\s+(\S+)\s+([i|s|f])\s+([pei])?\s+',true)

As written, the second token can't have whitespace. Is that what you  
intended?

You want ([isf]) for your third token. That will match a single  
instance of one of the three characters.

Also, I'm not clear on what you want for the fourth token. Do you want  
0 or 1 of [pei] or 0 or 1 of \*each\* of [pei] - which of these are  
valid for the fourth token: e or pe.

You should also end the regex with \s\* unless there's always going to  
be whitespace. You're currently checking for one or more whitespace  
characters at the end, but your test didn't have any.

Assuming you do allow 0 or 1 of each of [pei] for the fourth token and  
0 or more whitespace characters at the end, I would use:

r = /(add)\s+(\S+)\s+([isf])\s+(p?e?i?)\s\*/i

Though, if there can't be whitespace in any of the tokens, you might  
want to use split rather than a regex. The regex above can only get  
the fourth token right if the letters are in order - p will match, ei  
will match, ip will not. Split wouldn't have that problem. There may  
be a regex way around that, but I can't think of it at the moment.

"Add comp i ep".scan r # =\> [["Add", "comp", "i", "e"]]  
"Add comp i ep".split # =\> ["Add", "comp", "i", "ep"]

> **···**
>
> On Jan 13, 5:41 pm, Junkone \<junko...@gmail.com\> wrote:
> 
> > On Jan 13, 4:25 pm, Junkone \<junko...@gmail.com\> wrote:
> > 
> > \> Hello  
> > \> I have with great effort started on teh regex implemention and am  
> > \> stuck at one point.  
> > \> here is my code  
> > \> (add)\s+\S+\s+[i|s|f]\s+[pei]?\s+
> > 
> > \> The string is as follows and the tokens are equavalent to  
> > \> 1 token=Add  
> > \> 2 token=any text  
> > \> 3. token=one of the following i or s or f  
> > \> 4.token= 0 or 1 of the following characters [p e i]
> > 
> > \> I am stuck at the 4th token. i thought ? after the token will give  
> > \> the 0 or 1 option.  
> > \> pl correct me if i made a error
> > 
> > \> seede
> > 
> > irb(main):046:0\> a=Regexp.new('(add)\s+(\S+)\s+([i|s|f])\s+([pei])?\s  
> > +',true)  
> > =\> /(add)\s+(\S+)\s+([i|s|f])\s+([pei])?\s+/i  
> > irb(main):047:0\> a =~("Add comp i pe")  
> > =\> nil
> > 
> > I dont get match at 4th token
