# Regular expression

**URL:** <https://www.ruby-forum.com/t/regular-expression/169094>\
**Category:** Ruby\
**Created:** [May 29, 2009, 10:30am UTC](https://www.ruby-forum.com/t/regular-expression/169094 "2009-05-29T10:30:17Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [May 29, 2009, 10:30am UTC](https://www.ruby-forum.com/t/regular-expression/169094/1 "2009-05-29T10:30:17Z")

</div>

I want to write a regular expression to do the following.

The first character can be any character.  
The second character can be anything except the first character.  
The third character can be anything except the first two characters.  
(This is a problem. There may be other problems.)  
The fourth character is the same as the second character.

st = “abcb”  
p st =~ /^(.)([^\1])([^\1\2])(\2)$/ # I expected a match

uv = “abbb”  
p uv =~ /^(.)([^\1])([^\1\2])(\2)$/ # I did not expect a match

They both match.  
How can I write the regular expression?  
Somehow I have the syntax wrong.

Harry

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [May 29, 2009, 10:48am UTC](https://www.ruby-forum.com/t/regular-expression/169094/2 "2009-05-29T10:48:23Z")

</div>

Harry K. wrote:

> Somehow I have the syntax wrong.

Yes - I don’t think a backreference like \1, which could contain any  
number of characters, is usable inside a character class […], which is  
a list of individual characters.

irb(main):005:0\> /[^\1]/ =~ “a”  
=\> 0  
irb(main):006:0\> /[^\1]/ =~ “1”  
=\> 0  
irb(main):007:0\> /[^\1]/ =~ “\001”  
=\> nil

So it seems that [^\1\2] means any character apart from \001 (ctrl-A) or  
\002 (ctrl-B)

I think you need a negative lookahead assertion.

- [Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/)
- click on “The Ruby Language”
- scroll to “Extensions”
- look for (?!re)

irb(main):009:0\> /^(.)(?!\1)(.)(?!\1|\2)(.)(\2)$/ =~ “abcb”  
=\> 0  
irb(main):010:0\> /^(.)(?!\1)(.)(?!\1|\2)(.)(\2)$/ =~ “abbb”  
=\> nil

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [May 29, 2009, 11:50am UTC](https://www.ruby-forum.com/t/regular-expression/169094/3 "2009-05-29T11:50:36Z")

</div>

2009/5/29 Harry K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> uv = “abbb”  
> p uv =~ /^(.)([^\1])([^\1\2])(\2)$/ # I did not expect a match
> 
> They both match.  
> How can I write the regular expression?  
> Somehow I have the syntax wrong.

I second Brian: to solve this with a regular expression is either  
extremely costly (large expression) or even impossible. If I  
understand you properly then you want to avoid repetitions. How  
about:

require ‘set’  
def unique\_chars(s)  
chars = s.scan /./  
chars.to\_set.size == chars.size  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [May 29, 2009, 12:52pm UTC](https://www.ruby-forum.com/t/regular-expression/169094/4 "2009-05-29T12:52:46Z")

</div>

On Fri, May 29, 2009 at 6:49 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > p st =~ /^(.)([^\1])([^\1\2])(\2)$/ # I expected a match  
> > extremely costly (large expression) or even impossible. If I
> 
> robert
> 
> –  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

Thanks, Robert.

Sometimes I want to avoid repetition and sometimes I want to have  
repetition.  
My goal here is not just to solve such a problem.  
My goal is to learn how to write this type of regular expression using  
\1, \2, etc.  
I did not understand the syntax.

Thanks,

Harry

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [May 29, 2009, 1:55pm UTC](https://www.ruby-forum.com/t/regular-expression/169094/5 "2009-05-29T13:55:37Z")

</div>

2009/5/29 Harry K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On Fri, May 29, 2009 at 5:48 PM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I think you need a negative lookahead assertion.
> > 
> > - [Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/)
> > - click on “The Ruby Language”
> > - scroll to “Extensions”
> > - look for (?!re)
> > 
> > irb(main):009:0\> /^(.)(?!\1)(.)(?!\1|\2)(.)(\2)$/ =~ “abcb”  
> > =\> 0  
> > irb(main):010:0\> /^(.)(?!\1)(.)(?!\1|\2)(.)(\2)$/ =~ “abbb”  
> > =\> nil

> I need to read that section carefully to be sure I understand.  
> But, I think that does the trick.

The problem with this approach is that it does not work for strings of  
arbitrary length. Even if you adjust it to work for multiple lengths  
you always have a fixed upper limit for which it can work.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [May 29, 2009, 1:13pm UTC](https://www.ruby-forum.com/t/regular-expression/169094/6 "2009-05-29T13:13:18Z")

</div>

On Fri, May 29, 2009 at 5:48 PM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> =\> 0
> 
> - look for (?!re)
> 
> ## irb(main):009:0\> /^(.)(?!\1)(.)(?!\1|\2)(.)(\2)$/ =~ “abcb” =\> 0 irb(main):010:0\> /^(.)(?!\1)(.)(?!\1|\2)(.)(\2)$/ =~ “abbb” =\> nil
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Brian,

I need to read that section carefully to be sure I understand.  
But, I think that does the trick.

Thank you!

Harry

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [May 29, 2009, 2:20pm UTC](https://www.ruby-forum.com/t/regular-expression/169094/7 "2009-05-29T14:20:18Z")

</div>

Robert K. wrote:

> > > irb(main):009:0\> /^(.)(?!\1)(.)(?!\1|\2)(.)(\2)$/ =~ “abcb”  
> > > =\> 0  
> > > irb(main):010:0\> /^(.)(?!\1)(.)(?!\1|\2)(.)(\2)$/ =~ “abbb”  
> > > =\> nil
> 
> The problem with this approach is that it does not work for strings of  
> arbitrary length. Even if you adjust it to work for multiple lengths  
> you always have a fixed upper limit for which it can work.

The OP explicitly said that he wanted to match single characters. It  
would also make sense for other fixed-width fields, or delimited fields.

With neither fixed sizes nor delimiters, I don’t think it makes any  
sense. It would become “match any sequence of characters, followed by  
any sequence of characters which isn’t the same as the first sequence of  
characters, followed by any sequence of characters which isn’t the first  
or second, followed by the second sequence of characters”. And there  
would be a squillion different ways to try and slice the string to make  
it match.

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [May 29, 2009, 2:33pm UTC](https://www.ruby-forum.com/t/regular-expression/169094/8 "2009-05-29T14:33:12Z")

</div>

2009/5/29 Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> The OP explicitly said that he wanted to match single characters. It  
> would also make sense for other fixed-width fields, or delimited fields.

But he did not state explicitly that the _strings_ that he wanted to  
analyze have fixed length. This is where your approach breaks. If  
his strings are always of maximum length four or he only needs to make  
sure the four first characters do not repeat then it will work.  
“Four” could easily be any number up to “nine” but even then naming of  
groups might cause trouble; in any case the expression will soon look  
ugly, especially if strings can be “up to N” characters long.

> With neither fixed sizes nor delimiters, I don’t think it makes any  
> sense. It would become “match any sequence of characters, followed by  
> any sequence of characters which isn’t the same as the first sequence of  
> characters, followed by any sequence of characters which isn’t the first  
> or second, followed by the second sequence of characters”. And there  
> would be a squillion different ways to try and slice the string to make  
> it match.

That’s beyond the power of regular expressions.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [May 29, 2009, 4:19pm UTC](https://www.ruby-forum.com/t/regular-expression/169094/9 "2009-05-29T16:19:26Z")

</div>

Robert K. wrote:

> 2009/5/29 Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > The OP explicitly said that he wanted to match single characters. It  
> > would also make sense for other fixed-width fields, or delimited fields.
> 
> But he did not state explicitly that the _strings_ that he wanted to  
> analyze have fixed length. This is where your approach breaks.

But he’s not just looking for strings with non-repeating characters.  
Note the rule that says the fourth character must be equal to the second  
character. This is an application-specific rule; I don’t think we can  
logically extend that to guess what the rule for the fifth or subsequent  
characters would be.

> > With neither fixed sizes nor delimiters, I don’t think it makes any  
> > sense. It would become “match any sequence of characters, followed by  
> > any sequence of characters which isn’t the same as the first sequence of  
> > characters, followed by any sequence of characters which isn’t the first  
> > or second, followed by the second sequence of characters”. And there  
> > would be a squillion different ways to try and slice the string to make  
> > it match.
> 
> That’s beyond the power of regular expressions.

It’s perfectly possible to express it as a regular expression, but you  
have to beware that there could be many ways for it to match, so you  
might not get what you expect.

> > /^(.+)(?!\1)(.+)(?!\1|\2)(.+)(\2)$/.match(“foowibbleboingwibble”).to\_a  
> > =\> [“foowibbleboingwibble”, “foowibbl”, “e”, “boingwibbl”, “e”]  
> > /^(.+?)(?!\1)(.+?)(?!\1|\2)(.+?)(\2)$/.match(“foowibbleboingwibble”).to\_a  
> > =\> [“foowibbleboingwibble”, “foo”, “wibble”, “boing”, “wibble”]

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [June 1, 2009, 9:52am UTC](https://www.ruby-forum.com/t/regular-expression/169094/10 "2009-06-01T09:52:57Z")

</div>

I’m not sure if you still want the regex, but following regex should do  
the trick: ^(.{1})(?!\1)(.{1})(?!\1|\2).{1}\2$

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [May 29, 2009, 5:42pm UTC](https://www.ruby-forum.com/t/regular-expression/169094/11 "2009-05-29T17:42:03Z")

</div>

2009/5/29 Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> character. This is an application-specific rule; I don’t think we can  
> logically extend that to guess what the rule for the fifth or subsequent  
> characters would be.

Well, yes. I just did it but I see that the conclusion is not  
necessarily a valid one. So let’s hope for a better requirements spec  
next time. 🙂

Cheers

robert

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [June 1, 2009, 10:44am UTC](https://www.ruby-forum.com/t/regular-expression/169094/12 "2009-06-01T10:44:57Z")

</div>

Mark K. wrote:

> I’m not sure if you still want the regex, but following regex should do  
> the trick: ^(.{1})(?!\1)(.{1})(?!\1|\2).{1}\2$

Which is the same as the one I posted in my initial reply:

irb(main):009:0\> /^(.)(?!\1)(.)(?!\1|\2)(.)(\2)$/ =~ “abcb”  
=\> 0  
irb(main):010:0\> /^(.)(?!\1)(.)(?!\1|\2)(.)(\2)$/ =~ “abbb”  
=\> nil

---

<div class="post-metadata">

**Author:** ![duffbeer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@duffbeer](https://www.ruby-forum.com/u/duffbeer)\
**Post date:** [June 1, 2009, 12:09pm UTC](https://www.ruby-forum.com/t/regular-expression/169094/13 "2009-06-01T12:09:27Z")

</div>

Damn, you’re right… sorry 🙂
