gvp16 Posted February 17, 2012 Share Posted February 17, 2012 Hi, ive not done much with regex, but have started trying to use it with no lucj I have a list of product codes that match a few patterns, eg : 3641-001 ( part & colour) 01-1234 // not important 1234CS (part, colour, size) 123456 // not important im trying to right a function that takes these patterns in to account, eg. if(part_no like '0000-000') { do something } if(part_no like '00-0000') { do something else } can anyone help? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/257182-regex-newbie/ Share on other sites More sharing options...
AyKay47 Posted February 17, 2012 Share Posted February 17, 2012 Do you only care about the "3641-001" in "3641-001 ( part & colour)"? If so, are all of the part numbers followed by a space and then a parenthesis? Quote Link to comment https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1318319 Share on other sites More sharing options...
gvp16 Posted February 17, 2012 Author Share Posted February 17, 2012 yes just the numbers 3641-001 Thansks Quote Link to comment https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1318324 Share on other sites More sharing options...
AyKay47 Posted February 17, 2012 Share Posted February 17, 2012 can you give me an example of a full string that contains the text that you want to grab. Quote Link to comment https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1318326 Share on other sites More sharing options...
gvp16 Posted February 17, 2012 Author Share Posted February 17, 2012 its more of a match rather can grab, so if a part number matches this pattern: 4 digits a hyphen then 3 digits 0000-000, there is no other information in the string other than the part number. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1318327 Share on other sites More sharing options...
AyKay47 Posted February 17, 2012 Share Posted February 17, 2012 from the specifics that you have given me, your pattern will look like this: $str = "3641-001 ( part & colour)"; $pattern = "~\b\d{4}-\d{3}\b~"; preg_match($pattern, $str, $ms); print_r($ms); results: 3641-001 Quote Link to comment https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1318332 Share on other sites More sharing options...
gvp16 Posted February 17, 2012 Author Share Posted February 17, 2012 Thanks thats great, but i think I need to explain what i need to do a little better, I have a field in a database full of part numbers (9000+) im pulling them out with standard sql query so i have a variable to work with $row["Part_No"]; each type of product has a different pattern to the part number : eg . components have a number like : 0000-000 clothing have a number like : 0000AS (A = colour code can be anything from A - Z, S = Size can be anything from S,M,L,XL,XXL) and spares have a number like : 00-0000 when looping through the codes I need to determine which type of product it is so i can do different things to them if($row["Part_No"] matches patten 0000-00) { do stuff } if($row["Part_No"] matches patten 00-0000) { do stuff } if($row["Part_No"] matches patten 0000CM) { do stuff } Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1318339 Share on other sites More sharing options...
AyKay47 Posted February 17, 2012 Share Posted February 17, 2012 there are a couple of ways that this can be done. My preferred method would be to take the full results set, and filter the values similar to the logic outline that you have laid out. Since the values are coming from a database as a few expected patterns, the regex can be a little looser. $str = $row['Part_No']; if(preg_match('~\b\d{4}-\d{3}\b~',$str)) { // execute code for block 1 } else if(preg_match("~\b\d{4}[A-Z](?:S|M|L|XL|XXL)\b~",$str)) { //execute code for block 2 } else if(preg_match("~\b\d{2}-\d{4}\b~"),$str) { //execute code for block 3 } or you can self join using the clause REGEXP I'm sure there a several other ways, but I am not a mysql expert. Quote Link to comment https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1318344 Share on other sites More sharing options...
gvp16 Posted February 17, 2012 Author Share Posted February 17, 2012 that looks exactly what I was after, thanks very much Quote Link to comment https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1318345 Share on other sites More sharing options...
Adam Posted February 18, 2012 Share Posted February 18, 2012 You need to add ^ and $ to the patterns, otherwise they're useless. I could have: "I'm a dick but I like jam 0000AS" Read here for a good explanation: http://www.regular-expressions.info/anchors.html Quote Link to comment https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1318546 Share on other sites More sharing options...
AyKay47 Posted February 18, 2012 Share Posted February 18, 2012 You are right Adam, and I did this on purpose. Most of the time when I write a regex for someone on here, I will either leave something out or will have something slightly off to intrigue the OP to find the issue and fix it himself. Perhaps i should add that into my signature like ManiacDan.. I do appreciate you spotting it and linking the OP to an informative doc on the subject. Hopefully he/she will learn from it. Quote Link to comment https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1318588 Share on other sites More sharing options...
requinix Posted February 18, 2012 Share Posted February 18, 2012 Uh huh. Are those three the only possible patterns? Then you don't need regular expressions: just test for the differences between each one. Like components have a hyphen at the 5th position, spares have one at the 3rd position, and clothing is the odd one out. if ($row["Part_No"][4] == "-") { // component } else if ($row["Part_No"][2] == "-") { // spare } else { // clothing } Quote Link to comment https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1318642 Share on other sites More sharing options...
.josh Posted February 18, 2012 Share Posted February 18, 2012 You are right Adam, and I did this on purpose. Most of the time when I write a regex for someone on here, I will either leave something out or will have something slightly off to intrigue the OP to find the issue and fix it himself. Perhaps i should add that into my signature like ManiacDan.. I do appreciate you spotting it and linking the OP to an informative doc on the subject. Hopefully he/she will learn from it. If you are intentionally giving out wrong answers, that makes you a troll, purposely trying to sabotage the community. That is grounds for warning and ban. Quote Link to comment https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1318651 Share on other sites More sharing options...
AyKay47 Posted February 18, 2012 Share Posted February 18, 2012 You are right Adam, and I did this on purpose. Most of the time when I write a regex for someone on here, I will either leave something out or will have something slightly off to intrigue the OP to find the issue and fix it himself. Perhaps i should add that into my signature like ManiacDan.. I do appreciate you spotting it and linking the OP to an informative doc on the subject. Hopefully he/she will learn from it. If you are intentionally giving out wrong answers, that makes you a troll, purposely trying to sabotage the community. That is grounds for warning and ban. then ban me. Quote Link to comment https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1318659 Share on other sites More sharing options...
gvp16 Posted February 20, 2012 Author Share Posted February 20, 2012 Thanks for pointing out the error, as it happens in this case there wont be a long string with the part number so the code will work anyway. As for intentionally leaving something out, I think thats a bit mean, as the title suggests im newbie at using regex and after a few hours of googling trying to work it out my self I gave up and turned here for help. you should have mentioned that it was incomplete and i had to finish it, after all it was a point in the right direction. requinix, thanks for the suggestion, I did think of something like that, but there are going to be between 10 and 20 patterns that I need to work on. Thanks for all the help anyway. Quote Link to comment https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1319131 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.