Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 3rd, 2009, 02:35 AM
riotx's Avatar
riotx riotx is offline
Off-topic
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Location: Geneva / Genève / Genf / Ginevra
Posts: 1,632 riotx User rank is General 6th Grade (Above 100000 Reputation Level)riotx User rank is General 6th Grade (Above 100000 Reputation Level)riotx User rank is General 6th Grade (Above 100000 Reputation Level)riotx User rank is General 6th Grade (Above 100000 Reputation Level)riotx User rank is General 6th Grade (Above 100000 Reputation Level)riotx User rank is General 6th Grade (Above 100000 Reputation Level)riotx User rank is General 6th Grade (Above 100000 Reputation Level)riotx User rank is General 6th Grade (Above 100000 Reputation Level)riotx User rank is General 6th Grade (Above 100000 Reputation Level)riotx User rank is General 6th Grade (Above 100000 Reputation Level)riotx User rank is General 6th Grade (Above 100000 Reputation Level)riotx User rank is General 6th Grade (Above 100000 Reputation Level)riotx User rank is General 6th Grade (Above 100000 Reputation Level)riotx User rank is General 6th Grade (Above 100000 Reputation Level)riotx User rank is General 6th Grade (Above 100000 Reputation Level)riotx User rank is General 6th Grade (Above 100000 Reputation Level)  Folding Points: 1920 Folding Title: Novice Folder
Time spent in forums: 3 Weeks 6 Days 18 h 22 m 12 sec
Reputation Power: 1363
Unique characters only in string

Hi,

Need help with a simple expression - it's driving me nuts though! Basically need to match words that have unique letters (although they can contain characters too) in them - i.e. "bent" would return a match, but "here" wouldn't.

Any ideas? Tons of rep for the winner

Reply With Quote
  #2  
Old February 3rd, 2009, 10:28 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Sarcky
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,909 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 1 Day 9 h 17 m 54 sec
Reputation Power: 6113
Hmm...I don't think it's possible to do this quickly. Plus, it's probably always going to be quicker to loop through the string one character at a time. In PHP:
PHP Code:
function has_duplicate_letters$string ) {
    
$len strlen($string);
    for ( 
$p 1$p $len$p++ ) {
        for ( 
$q 0$q $p$q++ ) {
            if ( 
$string[$p] == $string[$q] ) return true
        }
    }
    return 
false;

-Dan
__________________
HEY! YOU! Read the New User Guide and Forum Rules

"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

Reply With Quote
  #3  
Old February 3rd, 2009, 03:38 PM
prometheuzz prometheuzz is offline
User 165270
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 497 prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 936
Quote:
Originally Posted by riotx
Hi,

Need help with a simple expression - it's driving me nuts though! Basically need to match words that have unique letters (although they can contain characters too) in them - i.e. "bent" would return a match, but "here" wouldn't.


This regex will do the trick:

Code:
^(?:(.)(?!.*?\1))*$

Reply With Quote
  #4  
Old February 3rd, 2009, 05:05 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Sarcky
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,909 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 1 Day 9 h 17 m 54 sec
Reputation Power: 6113
That regexp may work in languages that support \1 references inside the match condition, which I don't normally use. However, it seems to only check that the first letter is unique, not all of them. Though without experience in that style of expression, I can't tell you.

-Dan

Reply With Quote
  #5  
Old February 3rd, 2009, 07:45 PM
requinix's Avatar
requinix requinix is offline
Still alive
Dev Shed God 16th Plane (12500 - 12999 posts)
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,855 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 5 Days 4 h 51 m 44 sec
Reputation Power: 8977
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
You don't normally use backreferences?

What Cavemann posted works - try it. Kinda complicated though.
Code:
(.)[^\1]*\1

[edit] This expression tests if something has a repeated character. If you want to test for uniqueness then just negate the result you get. In PHP
PHP Code:
if (!preg_match('/(.)[^\1]*\1/'$text)) {
    
// each character in $text is unique


Last edited by requinix : February 4th, 2009 at 04:00 AM.

Reply With Quote
  #6  
Old February 4th, 2009, 01:11 AM
prometheuzz prometheuzz is offline
User 165270
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 497 prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 936
Quote:
Originally Posted by requinix
...

What Cavemann posted works - try it.


Err, "Cavemann"?

Quote:
Originally Posted by requinix
Kinda complicated though.
Code:
(.)[^\1]*\1


Most PCRE regex engines I know don't accept back references inside character sets. May I ask how you tested yours (in what language and with what input)?
Besides, if that had worked, you only seem to be checking if one character is repeated once, not what the OP is looking for (checking if all characters are unique).

Last edited by prometheuzz : February 4th, 2009 at 03:07 AM.

Reply With Quote
  #7  
Old February 4th, 2009, 03:04 AM
prometheuzz prometheuzz is offline
User 165270
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 497 prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 936
Quote:
Originally Posted by ManiacDan
That regexp may work in languages that support \1 references inside the match condition, which I don't normally use.


I see you are familiar with PHP, which supports back references. Note that (nearly) all PCRE regex engines (like PHP's preg-functions) support them.

Quote:
Originally Posted by ManiacDan
However, it seems to only check that the first letter is unique, not all of them.


No, that is not correct.

Quote:
Originally Posted by ManiacDan
Though without experience in that style of expression, I can't tell you.

-Dan


No offence, but before commenting on something you don't fully understand, perhaps you should first try it?

Reply With Quote
  #8  
Old February 4th, 2009, 03:59 AM
requinix's Avatar
requinix requinix is offline
Still alive
Dev Shed God 16th Plane (12500 - 12999 posts)
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,855 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 5 Days 4 h 51 m 44 sec
Reputation Power: 8977
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Quote:
Originally Posted by prometheuzz
Err, "Cavemann"?

Sometimes when I refer to people I call them by a different name. For fun. No bad feelings.

The name Prometheus reminds me of an old clay animation called Prometheus and Bob. While the caveman is actually Bob (the alien is Prometheus) for some reason I remember it the other way around.
Since you repeated the last character in your name I repeated the last in mine too.

Thus "prometheuzz" -> "Cavemann" (capitalized because it's a name)

[edit] Yeah, I'll admit that was a bit of a stretch. Most of the time it's more obvious (like E-Oreo becoming just Oreo). [/edit]

Quote:
Originally Posted by Cavemann
Most PCRE regex engines I know don't accept back references inside character sets. May I ask how you tested yours (in what language and with what input)?

I tested with PHP's preg_ functions (PHP 5.2.8, PCRE 7.8). If I had Perl I would have tried that.
PHP Code:
 $words = array(
    
"there",
    
"foo",
    
"bar",
    
"was not"
);

foreach (
$words as $w) {
    echo 
"$w: ";
    
var_dump(preg_match('/(.)[^\1]*\1/'$w));


Quote:
Originally Posted by Cavemann
Besides, if that had worked, you only seem to be checking if one character is repeated once, not what the OP is looking for (checking if all characters are unique).

Right. It checks for repeated characters. If it fails this test then all characters are unique.

I have a "check if it's invalid" mentality (as opposed to "check if it's valid") and considering how OP asked for something that does the exact opposite I probably should have mentioned that the result of my regex should be inverted.
(That, and I don't like using lookaheads or lookbehinds if I don't need to.)

PS: In that other thread, when I said "{ and } are special characters" I was simplifying. They're just as special as . * and ? (that is, most of the time but not always).

Last edited by requinix : February 4th, 2009 at 04:07 AM. Reason: various small edits, couple bigger ones

Reply With Quote
  #9  
Old February 4th, 2009, 04:12 AM
prometheuzz prometheuzz is offline
User 165270
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 497 prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 936
Quote:
Originally Posted by requinix
Sometimes when I refer to people I call them by a different name. For fun. No bad feelings.

The name Prometheus reminds me of an old clay animation called Prometheus and Bob. While the caveman is actually Bob (the alien is Prometheus) for some reason I remember it the other way around.
Since you repeated the last character in your name I repeated the last in mine too.

Thus "prometheuzz" -> "Cavemann" (capitalized because it's a name)

[edit] Yeah, I'll admit that was a bit of a stretch. Most of the time it's more obvious (like E-Oreo becoming just Oreo). [/edit]


Ah, I noticed the double "n" in "Cavemann", but didn't know the animation. Thanks for the link. ; )

Quote:
Originally Posted by requinix
I tested with PHP's preg_ functions (PHP 5.2.8, PCRE 7.8). If I had Perl I would have tried that.


Hmm, Java's java.util.regex package (a high PCRE degree) does not support them. I would have guessed PHP's preg-functions wouldn't either, which is not the case!

Quote:
Originally Posted by requinix
PHP Code:
 $words = array(
    
"there",
    
"foo",
    
"bar",
    
"was not"
);

foreach (
$words as $w) {
    echo 
"$w: ";
    
var_dump(preg_match('/(.)[^\1]*\1/'$w));



Right. It checks for repeated characters. If it fails this test then all characters are unique.

I have a "check if it's invalid" mentality (as opposed to "check if it's valid") and considering how OP asked for something that does the exact opposite I probably should have mentioned that the result of my regex should be inverted.
(That, and I don't like using lookaheads or lookbehinds if I don't need to.)


Since your "raw" regex pattern only matched a single character, I couldn't see it working. But it seems that the if() statement in in PHP does a bit more than I would think (I know very little PHP...).
Anyway, thank you for you clarification on the nickname and your example!
; )

Last edited by prometheuzz : February 4th, 2009 at 04:13 AM. Reason: Sticky fingers!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Unique characters only in string

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap