Regex Problem |
[eluser]Adam Griffiths[/eluser]
Hey guys, I have this really small, really niggly problem. I am using the twitter API and it returns the author like this:- author_name (author_name) - and I need to strip out the second author_name from within the parenthesis as well as the parenthesis. Here is my regex: Code: $author2 = $result = preg_replace('`\([:alnum]\)*\]`','',$AUTHOR['NAME']); I found it online somewhere and it didn't work when I tried it, so I played with it and put in :alnum instead of a-zA-Z0-9. Yeah so, any ideas? Thanks!
[eluser]jalalski[/eluser]
Shouldn't it be "[:alnum:]"? (Written without checking any references, so I may be way off the mark... ![]()
[eluser]Adam Griffiths[/eluser]
Wikipedia told me it was :alnum. I tried :alnum: but it threw a couple of errors at me. Code: Warning: preg_replace() [function.preg-replace]: Compilation failed: POSIX named classes are supported only within a class at offset 2 in /home/www/twitvine/public/twitter.php on line 144
[eluser]Hockeychap[/eluser]
If you are using preg_replace, then you'll want to use the following as your pattern to match: $pattern = "/\(\w+\)/"; or in english : 1. Match the literal ( 2. Match a single character that is a word character (letters and digits etc) between 1 and unlimited times 3. Match the literal ) You can then simply use a blank as the substitution string to remove the 2nd name. Hope this helps, Justin.
[eluser]Hockeychap[/eluser]
No worries. If you are doing a lot of regex work I find a product called "Regex Buddy" really handy as it supports Posix, PCRE, Java and others patterns and allows you to check your results on the fly. Sadly it's not free, but for the amount I use regex it was well worth the 30EUR fee. It's available here. Even if you don't buy the product it's a really useful regex reference site. Cheers Justin
|
Welcome Guest, Not a member yet? Register Sign In |