homepage Welcome to WebmasterWorld Guest from 54.196.69.189
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderators: physics

Databases Forum

    
how to compare two instances using MySQL REGEXP Function
how to compare two instances using MySQL REGEXP Function
parorrey




msg:1578703
 5:46 pm on Dec 22, 2005 (gmt 0)

hi,
can somebody tell me what m i doing wrong in this MySQL statement.
i want to compare '/' at the start and end of the string in my Link field which can have any chracters from 'a-z' and '-' and it cant have '/' again in the middle of string, just at start and end. i ve tried the following statement and other variations of it but to no avail.


SELECT Name, Link FROM Table WHERE Link REGEXP '^/[a-z-]/$'

please help.

 

coopster




msg:1578704
 10:51 pm on Dec 22, 2005 (gmt 0)

Are you trying to find one or more instances of those characters? Use the plus sign. Use the asterisk for zero or more.
SELECT Name, Link FROM Table WHERE Link REGEXP '^/[a-z-]+/$';

parorrey




msg:1578705
 3:27 am on Dec 23, 2005 (gmt 0)

exactly & that worked like a charm. :D
on another note, what if we want to exclude '/' from the string,
for instance, i ve records '/category/' or '/detailed-name-category/' which i want to match but i also have such records like '/category/subcategory/' or '/detailed-name-category/detailed-name-subcategoery/' which i dont want to match, and in your provided statement they dont, is this statment
SELECT Name, Link FROM Table WHERE Link REGEXP '^/[a-z-]+/$';

explicitly exclude '/' from the string apart from start and end so that only categories match and not the subcat or any other records.
but this statement does what i want to do in anycase, i m just trying to dig a bit deeper.
thnx

parorrey




msg:1578706
 7:45 am on Dec 23, 2005 (gmt 0)

ok , got it now completely,

SELECT Name, Link FROM Table WHERE Link REGEXP '^/[a-z-]+/$';

in the aboive staement, we are explicitly defining which chracters to match [a-z-]+ and it skipped '/' in the middle of string.
thnx.

coopster




msg:1578707
 3:12 pm on Dec 23, 2005 (gmt 0)

Exactly. Since the slash character (/) is not represented in the character class (the *stuff* in between the brackets ([]) ) then it is not a valid matching character. And the caret (^) and dollar sign are anchors, they say that the pattern must begin with the anchor specified as well as end with the anchor specified -- in this case it is the slash character.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved