dsaba Posted December 5, 2007 Share Posted December 5, 2007 yay! finally the question/s you've been waiting for! -It's not too specific on "how do I fix bla bla, in my own specific project..bla bla" -Its general enough to help onlookers on regex questions in general -------------------------------------------------------------------------------------- I want to match something like this: <meta name="stapler" content="Whatever 39.32" /> the part that will change that I cannot take literally will be the "39.32" So you can basically think of it like this: literal text - any number of chars less than 50 - literal text sounds pretty easy right? I thought so too. Here's my attempt: $pattern = "~literal text .+{1,50} literal text~"; all I want to say is match any characters and 50 or less of them i've seen it written this way before: (.*?){0,200} the dot means 1 single char of any kind the asterisk means 0 or more of these characters and the question mark means optional of these characters, meaning it can be 0?? instead of putting the ? mark there why not just leave it as: (.*) -- I mean doesn't the 0 or more part basically say its optional too? Since there can be 0 I don't quite get the brackets either if someone could explain, when and when not to use them, i tried saying: .*{0,50} but this gave some repitition error..etc.. -thanks Quote Link to comment https://forums.phpfreaks.com/topic/80218-some-questions-about-metacharacters-and-their-usage/ Share on other sites More sharing options...
BenInBlack Posted December 5, 2007 Share Posted December 5, 2007 * means - Zero or more of 'n' {0,50} mean Zero to 50 of 'n' you can combine them it is either .* or .{0,50} here is a regex tester I like to use http://www.quanetic.com/regex.php Quote Link to comment https://forums.phpfreaks.com/topic/80218-some-questions-about-metacharacters-and-their-usage/#findComment-406669 Share on other sites More sharing options...
dsaba Posted December 5, 2007 Author Share Posted December 5, 2007 I want to match the "123" whatever 123 this ~whatever .* this~ ~whatever (.){0,50} this~ none of these work, what will? Quote Link to comment https://forums.phpfreaks.com/topic/80218-some-questions-about-metacharacters-and-their-usage/#findComment-406828 Share on other sites More sharing options...
effigy Posted December 5, 2007 Share Posted December 5, 2007 and the question mark means optional of these characters, meaning it can be 0?? No. When a question mark follows a quantifier it makes it ungreedy (lazy). Quote Link to comment https://forums.phpfreaks.com/topic/80218-some-questions-about-metacharacters-and-their-usage/#findComment-406906 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.