x1nick Posted October 7, 2009 Share Posted October 7, 2009 This should be really simple, but can't work it out. I have multiple values eg {username} in a string. Inside the { } brackets, valid charachters are a-z (upper and lower) 0-9 and _ preg_match_all('/{([^}]*)}/',$var,$varmatch); Is my current code, but need to limit this to the above characters only. What expression should I use? Link to comment https://forums.phpfreaks.com/topic/176833-solved-preg_match_all-simple-question/ Share on other sites More sharing options...
cags Posted October 7, 2009 Share Posted October 7, 2009 I haven't tested it but how about... '~{[A-Za-z0-9_]*?}~' This may also work, but unfortunately off the top of my head I'm not certain what characters \w matches, I know it's word characters, but this may well include characters you don't want like apostrophe and hyphen. '~{[\w\d_]*?}~' Link to comment https://forums.phpfreaks.com/topic/176833-solved-preg_match_all-simple-question/#findComment-932356 Share on other sites More sharing options...
x1nick Posted October 7, 2009 Author Share Posted October 7, 2009 Neither work with the braces attached. '/{~{[A-Za-z0-9_]*?}~}/' Have I applied that right? Link to comment https://forums.phpfreaks.com/topic/176833-solved-preg_match_all-simple-question/#findComment-932359 Share on other sites More sharing options...
salathe Posted October 7, 2009 Share Posted October 7, 2009 What expression should I use? preg_match_all('/{([a-zA-Z0-9_]*)}/', $var, $varmatch); Link to comment https://forums.phpfreaks.com/topic/176833-solved-preg_match_all-simple-question/#findComment-932361 Share on other sites More sharing options...
x1nick Posted October 7, 2009 Author Share Posted October 7, 2009 Works perfectly. Just to clear up does the * return the content's between the braces? Really having trouble getting my head round regex! Link to comment https://forums.phpfreaks.com/topic/176833-solved-preg_match_all-simple-question/#findComment-932365 Share on other sites More sharing options...
nrg_alpha Posted October 7, 2009 Share Posted October 7, 2009 Iunfortunately off the top of my head I'm not certain what characters \w matches, I know it's word characters, but this may well include characters you don't want like apostrophe and hyphen. '~{[\w\d_]*?}~' Without going into locale issues (which have direct ramifications on what stuff like \w includes), for all intents and purposes, \w = [a-zA-Z0-9_] So in your snippet there, you have added \d and _ for nothing (now that you know what constitutes as \w (more or less / basically). Just to clear up does the * return the content's between the braces? No, the * is known as a 'zero or more times' quantifier (a quantifier, well, quantifies something). So if you have a pattern like /a*b/, end results could include: b ab aab aaaaab aaaaaaaaaaab etc... You can read up on regex in the following links to help you better understand it: http://www.phpfreaks.com/tutorial/regular-expressions-part1---basic-syntax http://www.regular-expressions.info/ http://weblogtoolscollection.com/regex/regex.php http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1239475391&sr=8-1 Link to comment https://forums.phpfreaks.com/topic/176833-solved-preg_match_all-simple-question/#findComment-932374 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.