Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a regular expression http\:\/\/domainname\.com\/\S{4} which should catch urls like this: http://domainname.com/key4 in a longer text.

I want to get only the key and match it with a table field in my postgres database.

after trying some stuff I came to this query (for grabbing keys):

SELECT substring(infos FROM '[http\:\/\/domainname\.com\/\S{4}]{7}' ) AS key FROM actions

as a result i get the /domainname.com for each row ... well, no keys as you can see.

what am I doing wrong?

can anyone tell me what the {7} stands for?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

The {7} stands for 7 times the previous pattern. So in this case, the different characters between [ and ]. i.e. [abc]{3} matches aaa cba or any other combination.

I'm fairly certain that this is not what you want. You are probably looking for something like this instead:

SELECT substring('http://domainname.com/key4' from 'http://domainname\\.com/\\S{4}')
FROM actions
share|improve this answer
 
i get NULL as results for every row... –  helle Feb 14 '11 at 14:00
 
@helle: can you give an example of how infos looks like? Cause it should work with the given expression (works with all testdata I've given it) –  Wolph Feb 14 '11 at 17:37
 
infos contains a JSON string. –  helle Feb 15 '11 at 15:28
add comment

This might be what you are looking for in this specific case

select substring(substring('long text [http://domainname.com/key4] more text',E'http\:\/\/domainname\.com\/\\S{4}'),E'\\S{4}$');

which will extract the key part if it is four characters long. If you might also be looking for something like key456 then a more generic match such as

select substring(substring('long text [http://domainname.com/key467] more text',E'http\:\/\/domainname\.com\/key\\d+'),E'key\\d+$');

may be more appropriate

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.