![]() |
Regex Problem - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Regex Problem (/showthread.php?tid=14549) |
Regex Problem - El Forum - 01-06-2009 [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! Regex Problem - El Forum - 01-06-2009 [eluser]jalalski[/eluser] Shouldn't it be "[:alnum:]"? (Written without checking any references, so I may be way off the mark... ![]() Regex Problem - El Forum - 01-06-2009 [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 Regex Problem - El Forum - 01-07-2009 [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. Regex Problem - El Forum - 01-07-2009 [eluser]Adam Griffiths[/eluser] Thanks Justin, it worked like a charm! Regex Problem - El Forum - 01-07-2009 [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 Regex Problem - El Forum - 01-07-2009 [eluser]Adam Griffiths[/eluser] Thanks for the tip. |