RegEx Woes! Please Help, Simple Question

F

frans abels

Hi Gurus,

I got a quick question on regex. My input string consists of two
characters: [a-z][a-z].. However, I only want to match if the two
characters are different.

Example: ab,ck,dk,gk... are all valid, but aa,bb,cc,dd,ee... should not
match..

This is the regex I have so far, but it does not seem to work:

([a-z])(?!\1)[a-z]

It seems to work for me. In perl at least, I don't know about java.

frans
 
S

SMalik786

Hi Gurus,

I got a quick question on regex. My input string consists of two
characters: [a-z][a-z].. However, I only want to match if the two
characters are different.

Example: ab,ck,dk,gk... are all valid, but aa,bb,cc,dd,ee... should not
match..

This is the regex I have so far, but it does not seem to work:

([a-z])(?!\1)[a-z]

Please Gurus, HELP!

Saad.
 
A

A. Sinan Unur

(e-mail address removed) wrote in @z14g2000cwz.googlegroups.com:
Hi Gurus,

I recommend reading the posting guidelines for this group. They contain
valuable information on how you can help us help you.
I got a quick question on regex. My input string consists of two
characters: [a-z][a-z].. However, I only want to match if the two
characters are different.

Example: ab,ck,dk,gk... are all valid, but aa,bb,cc,dd,ee... should
not match..

This is a good description.
This is the regex I have so far, but it does not seem to work:

([a-z])(?!\1)[a-z]

Is there a reason this has to be regex solution? I am fairly dimwitted
when it comes to regular expressions, and would have coded this as:

#! /usr/bin/perl

use strict;
use warnings;

while(<DATA>) {
print if /^([a-z])([a-z])$/ and ($1 ne $2);
}
__DATA__
aa
ab
cc
ca
Please Gurus, HELP!

I am sure you did not intend to cause offense, but this kind of flattery
is generally not needed here.

Sinan.
 
S

SMalik786

Thanks for that solution.

That will work for perl, but I'm actually using Java for my regex. Even
though the same technique could be done in Java, but I am also trying
to understand why my solution is not working..

Anyway, thanks again.
 
A

A. Sinan Unur

(e-mail address removed) wrote in @z14g2000cwz.googlegroups.com:
Thanks for that solution.

Which solution? Please quote some context in your replies. It looks like
you completely disregarded my suggestion to read the posting guidelines.
That will work for perl, but I'm actually using Java for my regex.

Then you are in the wrong group.

Sinan
 
J

Janek Schleicher

Thanks for that solution.

That will work for perl, but I'm actually using Java for my regex. Even
though the same technique could be done in Java, but I am also trying to
understand why my solution is not working..

I would say that it is really a Java question, as Perl handles your regexp
correct, e.g.

my @s = qw/ab aa xy/;

print join " ", grep /([a-z])(?!\1)[a-z]/, @s;

prints
ab xy
as expected.

Thus, I would advice you to ask your question in a Java newsgroup, as your
regexp works in Perl.


Greetings,
Janek
 
F

Fabian Pilkowski

Thanks for that solution.

Please quote the part of the posting you're answering to. It makes
easier to read -- and one hasn't to write so much ;-)

Have you tested Sinan's solution with Perl? Please do it! After doing
so, take your regex and replace Sinan's with that. You'll get code like


#! /usr/bin/perl -w
use strict;
while ( <DATA> ) {
print if /([a-z])(?!\1)[a-z]/;
}
__DATA__
aa
ab
cc
ca


Then run it through your Perl interpreter and be amazed. It does what
you want.
That will work for perl, but I'm actually using Java for my regex. Even
though the same technique could be done in Java, but I am also trying
to understand why my solution is not working..

Your regex will work for Perl too. I think it's a problem with the used
zero-width negative look-ahead assertion "(?!pattern)". This is an
extended pattern Perl provides -- is this supported by Java at all? I
propose you ask this again in a group concerns with Java since you have
already two Perl solutions ;-)

Anyway, try to implement Sinan's code in Java -- I assume Java supports
such simple regexes as well.

regards,
fabian
 
A

A. Sinan Unur

so, take your regex and replace Sinan's with that.

My face is a rather deep shade of red right now. I did not even check the
OP's code, and took his word that it did not accomplish his stated aim. I
am sure there is a lesson for me to learn there.

Sinan
 
G

Gunnar Hjalmarsson

A. Sinan Unur said:
I got a quick question on regex. My input string consists of two
characters: [a-z][a-z].. However, I only want to match if the two
characters are different.

Example: ab,ck,dk,gk... are all valid, but aa,bb,cc,dd,ee... should
not match..

This is a good description.

True ...
----------------------------------------^^^^^^^^^^^^^^^^^^^^^^^^
.... but that's certainly not.

It works fine for me.

Please respect the posting guidelines for this group:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

and post a short but *complete* program that illustrates the claimed
behaviour.
 
F

Fabian Pilkowski

* A. Sinan Unur said:
My face is a rather deep shade of red right now. I did not even check the
OP's code, and took his word that it did not accomplish his stated aim. I
am sure there is a lesson for me to learn there.

IMHO, we all could learn from being a part of this newsgroup. But you've
already mentioned your dimwittedness concerning regexes. There's no need
to blush.

Not to checking his own code is a more serious delict. And it's not
really difficult to avoid this -- from that place I suggest you pass
your red-shaded face to Saad, the OP. ;-)

regards,
fabian
 
S

SMalik786

I apologize for posting to this group, its just that I did a search on
regular expressions (in Google Groups) and the majority of posts came
from this group..

In anycase I later posted same question to a java group, and someone
replied back informing me that I need to use two backward slashes (to
escape it from the compiler). So here is the correct RegEx IN JAVA that
works:

([a-z])(?!\\1)[a-z]

Once again, I apologize.
Saad
 
J

Janek Schleicher

I apologize for posting to this group, its just that I did a search on
regular expressions (in Google Groups) and the majority of posts came from
this group..

In anycase I later posted same question to a java group, and someone
replied back informing me that I need to use two backward slashes (to
escape it from the compiler). So here is the correct RegEx IN JAVA that
works:

([a-z])(?!\\1)[a-z]

To be a bit pedantic,
the correct Java regexp is also
([a-z])(?!\1)[a-z]
the only difference is that you have to fix your regexp in Java as a
String where backslashes have a special meaning (what in Perl is not
necessary), thus you have to protect the backslash for the string, not for
the regexp. "\\" stands in Java only for _one_ backslash.


Greetings,
Janek
 
B

Bart Lateur

Fabian said:
Have you tested Sinan's solution with Perl? Please do it! After doing
so, take your regex and replace Sinan's with that. You'll get code like


#! /usr/bin/perl -w
use strict;
while ( <DATA> ) {
print if /([a-z])(?!\1)[a-z]/;
}
__DATA__
aa
ab
cc
ca


Then run it through your Perl interpreter and be amazed. It does what
you want.
That will work for perl, but I'm actually using Java for my regex. Even
though the same technique could be done in Java, but I am also trying
to understand why my solution is not working..

Your regex will work for Perl too. I think it's a problem with the used
zero-width negative look-ahead assertion "(?!pattern)". This is an
extended pattern Perl provides -- is this supported by Java at all?

Just let me add that it works in Javascript too.

<html><head><title>HTML test page</title>
</head><body>
<pre>
<script>
var test = ['aa', 'ab', 'cc', 'ca' ];
for (var i in test) {
var s = test;
if(s.match(/^([a-z])(?!\1)[a-z]/)) document.write(s + "\n");
}
</script></pre>
</body></html>

this shows:

ab
ca

in Firefox and in MSIE (5.5)

It's more widely accepted than one would think.
 
J

Joonas Timo Taavetti Kekoni

(e-mail address removed) wrote:
: That will work for perl, but I'm actually using Java for my regex.

Are you sure you handled the string escapes correctly?
with java you have to do \\ for \ in regexp and \" for ".
That makes sometimes simple things complicate.

In perl you can just:
print $1 if( /"[^"]*"/ );

Look at the java regexp syntax, do you use some feature it
does not support, it does not have all the features of java.

try:
"(a[b-z])|(b(a|[c-z]))|(c([a-b]|[d-z]))|(e([a-d]|[f-z])) ...
That should work ;-)

You are using:
(?!X) X, via zero-width negative lookahead
This seems to be supported by java,

I have never used it either with perl nor java, so
I cannot comment.

BTW:
Are you using this pattern
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
or some older, because there used to be 3rd party regexp libs 4 java
before JDK 1.4

And last but not least:
How bout doing some java code to do it...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top