j.smith1981 Posted May 25, 2011 Share Posted May 25, 2011 I have been trying to avoid learning Regular Expressions but feel I should really dig into them to be honest. I just wanted to know if any of you know the best tutorials that are out there on the web, or even book guides on the various approaches to doing good regular expressions. Just fancy buckling down and getting used to using them, want to make a username entry for a user sign up page for my project blog and fancied doing some of my own, just to teach myself really. Any reply help is much appreciated, Jeremy. Quote Link to comment https://forums.phpfreaks.com/topic/237435-php-regular-expressions-best-tutorials-web-or-books/ Share on other sites More sharing options...
j.smith1981 Posted May 25, 2011 Author Share Posted May 25, 2011 Woops I forgot you had a tutorial here: http://www.phpfreaks.com/tutorial/regular-expressions-part1---basic-syntax Going to have a look at that, but are there any others should I get stuck that are good around on the web, or could you recommend any books on this particular aspect of logic? But will for now read through the tutorial guides on this site, Thank you and apologies. Quote Link to comment https://forums.phpfreaks.com/topic/237435-php-regular-expressions-best-tutorials-web-or-books/#findComment-1220042 Share on other sites More sharing options...
xylex Posted May 25, 2011 Share Posted May 25, 2011 Jeffery Friedl's Mastering Regular Expressions book was a huge help getting things going for myself. Quote Link to comment https://forums.phpfreaks.com/topic/237435-php-regular-expressions-best-tutorials-web-or-books/#findComment-1220159 Share on other sites More sharing options...
j.smith1981 Posted May 26, 2011 Author Share Posted May 26, 2011 I am also looking at this aswell: http://www.codeproject.com/KB/dotnet/regextutorial.aspx But will of course give that book a look. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/237435-php-regular-expressions-best-tutorials-web-or-books/#findComment-1220486 Share on other sites More sharing options...
gizmola Posted May 26, 2011 Share Posted May 26, 2011 There's a great tool for helping test and learn regex http://weitz.de/regex-coach/ This site does a fantastic job covering the subject: http://www.regular-expressions.info/ Quote Link to comment https://forums.phpfreaks.com/topic/237435-php-regular-expressions-best-tutorials-web-or-books/#findComment-1220501 Share on other sites More sharing options...
j.smith1981 Posted May 26, 2011 Author Share Posted May 26, 2011 Thank you for that. Will give that a read also, infact the one I posed the link I mean, was not that good. But I know I can make them work for my own usage, which is why I am experimenting and it's making sense. Just out of something completely random got something like this working: <?php $string = 777.'-'; $string .= 7777777; $pattern = '/\d\d\d/'; // could start off with going along all the way its meant to be formatted, but that would be far too long! I have managed to better this by doing the following: <?php $string = 777.'-'; $string .= 7777777; $pattern = '/\d{3}-\d{7}/'; preg_match($pattern, $string, $matches); print_r($matches); That should output: Array ( [0] => 777-7777777 ) Though that is just an off the top of my head experiment. Quote Link to comment https://forums.phpfreaks.com/topic/237435-php-regular-expressions-best-tutorials-web-or-books/#findComment-1220520 Share on other sites More sharing options...
j.smith1981 Posted May 26, 2011 Author Share Posted May 26, 2011 When it says: You can easily try the following yourself in a text editor that supports regular expressions, such as EditPad Pro In this link: http://www.regular-expressions.info/tutorial.html Is Notepad++ a good regex editor at all, or does it have the capacity to do so? If not is there any other free ones available I can use? Quote Link to comment https://forums.phpfreaks.com/topic/237435-php-regular-expressions-best-tutorials-web-or-books/#findComment-1220531 Share on other sites More sharing options...
j.smith1981 Posted June 2, 2011 Author Share Posted June 2, 2011 Whilst on the hunt for a good free regexp editor, been playing around with various regex's. It's actually sticking too! Not being able to actually do very much except for literals but it's quite good now I am understanding somewhat what their actually doing rather than just thinking yes that would work but only relying purely on the author of a post, which I can not stand to do, if I start doing that I won't use it, even though it works, does anyone else do the same as me? Anyways enough the waffle, just wanted to show one I have customised on a very simple level though: http://www.webresourcesdepot.com/learn-test-regular-expressions-with-the-regulator/ I have then changed this slightly to: preg_match('/H(á|ae?)nd(a|ae?)l/', $string, $matches); Say I put in (as a literal): 'Hándael' it would find 3 occurences of the pattern in the string wouldn't it? H is a literal? (please correct me if I am wrong the whole point of me making this post is so I can compare what I believe to be true as such). ae is in the or with the | sign in brackets right? then the final 'l' is a literal (sorry please correct me if I am using the wrong terminology for what I am explaining). Would appreciate your feedback very much, just for memory what exactly is the metachar for anywhere in the string? Just out of interest. Alright I will leave it there, thank you and I appreciate any responses in advance, Jeremy. Quote Link to comment https://forums.phpfreaks.com/topic/237435-php-regular-expressions-best-tutorials-web-or-books/#findComment-1224143 Share on other sites More sharing options...
gizmola Posted June 2, 2011 Share Posted June 2, 2011 I have then changed this slightly to: preg_match('/H(á|ae?)nd(a|ae?)l/', $string, $matches); Say I put in (as a literal): 'Hándael' it would find 3 occurences of the pattern in the string wouldn't it? H is a literal? (please correct me if I am wrong the whole point of me making this post is so I can compare what I believe to be true as such). ae is in the or with the | sign in brackets right? then the final 'l' is a literal (sorry please correct me if I am using the wrong terminology for what I am explaining). Would appreciate your feedback very much, just for memory what exactly is the metachar for anywhere in the string? Right a literal, is "literally this character or string of characters." As for matches, you are looking at it the wrong way, the pattern either matches or it doesn't. There are not 3 occurrences of the pattern in the string you presented, there is one and it does match. However, this also matches: "Handal". If that surprises you you want to look at the 2 "or" cases: á|ae? What that question mark after the e is saying is that the "prior character" (the 'e') is optional when following an a. So the pattern will match any of: á a ae The other thing to understand is the use of parens: "()" In regex when you put something inside parens, unless you add a character that tells the regex engine not to, the portion of inside the parens will be captured. This is the key to doing search and replace, as each of these captured portions get numbered, and can then be used in substitutions. So when you look at preg_replace for example, or even the array you get in $matches, it is based on the captured elements you have in your regex. With that said, preg_match will either match a string or it won't. It returns the number of matches it is able to find. If you have no occurrence of the complete pattern you will not get a match. In other words, just because I have: "Hand" doesn't mean I'm going to get anything in $matches. Check this out: $string = 'Hand'; $matches = array(); $count = preg_match('/H(á|ae?)nd(a|ae?)l/', $string, $matches); echo "$count Matches for $string\n"; var_dump($matches); $string = "Handael"; $matches = array(); $count = preg_match('/H(á|ae?)nd(a|ae?)l/', $string, $matches); echo "$count Matches for $string\n"; var_dump($matches); Output: 0 Matches for Hand array(0) { } 1 Matches for Handael array(3) { [0]=> string(7) "Handael" [1]=> string(1) "a" [2]=> string(2) "ae" } Notice that in $matches, when there is a complete match available, the "submatches" get put into $matches. The first element is the complete match found, and the next 2 elements are the submatches for the 2 sets of parens in your regex. One last thing: notepad++ does not have a full featured regex implementation. Many other editors do, but notepad++ is not one of them. Quote Link to comment https://forums.phpfreaks.com/topic/237435-php-regular-expressions-best-tutorials-web-or-books/#findComment-1224328 Share on other sites More sharing options...
xylex Posted June 2, 2011 Share Posted June 2, 2011 One note on that Hándael example - it's a nice example for learning about regular expressions, but if you ever find yourself trying to match various ways someone might have written the same word using similar letters in Unicode, you'll usually be better off using the Intl class and learning ICU instead of trying to code out regular expressions to do it. Quote Link to comment https://forums.phpfreaks.com/topic/237435-php-regular-expressions-best-tutorials-web-or-books/#findComment-1224348 Share on other sites More sharing options...
j.smith1981 Posted June 3, 2011 Author Share Posted June 3, 2011 Thank you ever so much for all your replies, really truly appreciate them! I am learning so much about regex's now, thanks to your invaluable help. I mean the whole point of me coming on here is to learn about programming area's, really appreciate your replies. Will dig away around them, I mean with the unicode yes of course, but really all it was (and I think you realise this too) is that it was just entirely an example not really fussed about that for now, but will just dig into that if a case was to come up where I would need to do that and will go through the logic once again. Of how many times you can search a string etc, I do understand though that I am kind of burrowing myself into a corner with these, always thinking of how to extend such expressions. Like would it be best if I was to say only going to allow certain values into a regex, say like for a username validation, I was to just allow the following chars in them: . (full stop or period) _ (underscore) 0-9 a-z (if i wanted say A-Z, would I need to put the case insensitive metachar?) I noticed when I tried putting in a regular expression, with literally [a-zA-Z] it allowed me to put in all kinds of characters, wanted to keep a fine control on what the user can input into them. How would I allow all those quoted symbols anywhere and as many as they like in string? Might finally get me a slight push to get used to regex's. Again really appreciate any replies, Jeremy. Quote Link to comment https://forums.phpfreaks.com/topic/237435-php-regular-expressions-best-tutorials-web-or-books/#findComment-1224559 Share on other sites More sharing options...
j.smith1981 Posted June 3, 2011 Author Share Posted June 3, 2011 I mean sorry would I list all the symbols or say if any other tag than these (the quoted ones in my previous post) are the only allowed valid ones? How would I make the logic of a preg_match() take appreciation to this? Just going through all the logic in the world, eventually will make sense but it's just I am not getting productional really on my own basis, could you help please? I really fully appreciate the replies so far though! Thank you for reading, Jeremy. Quote Link to comment https://forums.phpfreaks.com/topic/237435-php-regular-expressions-best-tutorials-web-or-books/#findComment-1224596 Share on other sites More sharing options...
gizmola Posted June 3, 2011 Share Posted June 3, 2011 I think this thread has run its course. For questions about a particular application of regex please make a new thread. I have to admit that right now I don't understand what you are asking, so you might need to try and figure out a way to phrase the question that is clearer. Quote Link to comment https://forums.phpfreaks.com/topic/237435-php-regular-expressions-best-tutorials-web-or-books/#findComment-1224822 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.