imperium2335 Posted December 9, 2011 Share Posted December 9, 2011 Hi, How would I write a regex that would match words that have a mixture of numbers, letters and symbols but no spaces. e.g.: "I would like 1 LG1K065Q704" Quote Link to comment https://forums.phpfreaks.com/topic/252802-how-to-match-a-part-number/ Share on other sites More sharing options...
OOP Posted December 9, 2011 Share Posted December 9, 2011 Hi there You can use this "^[a-zA-Z0-9]*$" Quote Link to comment https://forums.phpfreaks.com/topic/252802-how-to-match-a-part-number/#findComment-1296080 Share on other sites More sharing options...
imperium2335 Posted December 9, 2011 Author Share Posted December 9, 2011 Thanks, but it didn't work That goes ahead and matches most things. I tried /[a-z][A-Z][0-9]/i which seems to work but since I did it I don't think it will be that reliable, what do you think? Quote Link to comment https://forums.phpfreaks.com/topic/252802-how-to-match-a-part-number/#findComment-1296082 Share on other sites More sharing options...
OOP Posted December 9, 2011 Share Posted December 9, 2011 The regex I have provided above will match any alphanumeric? This is what you want, right? change it to this and it will not accept empty string "^[a-zA-Z0-9]+$" Quote Link to comment https://forums.phpfreaks.com/topic/252802-how-to-match-a-part-number/#findComment-1296084 Share on other sites More sharing options...
imperium2335 Posted December 9, 2011 Author Share Posted December 9, 2011 But I only want it to match the product code LG1K065Q704, and no other word in the paragraph. In pseudo: match a word that MUST have numbers and letters and/or symbols and no spaces in it. Quote Link to comment https://forums.phpfreaks.com/topic/252802-how-to-match-a-part-number/#findComment-1296086 Share on other sites More sharing options...
OOP Posted December 9, 2011 Share Posted December 9, 2011 Okay, Is your product code random? or does it have to start with character? can you specify the format you want to validate? Quote Link to comment https://forums.phpfreaks.com/topic/252802-how-to-match-a-part-number/#findComment-1296091 Share on other sites More sharing options...
imperium2335 Posted December 9, 2011 Author Share Posted December 9, 2011 product codes could be anything, usually greater than 4 characters long. This is for something that will find part codes in emails from clients, so they could be like this: "I want 1 123-abc-ppr5" or "55-33-AB/8 x5" Quote Link to comment https://forums.phpfreaks.com/topic/252802-how-to-match-a-part-number/#findComment-1296095 Share on other sites More sharing options...
xyph Posted December 9, 2011 Share Posted December 9, 2011 <?php $expr = '/(?:^| )((?:[0-9]+[a-z]|[a-z]+[0-9])[a-z0-9]+)/si'; $str = 'hahaha ae232a4d zomg wut wut wut asgfe 3231aa3dw'; preg_match_all( $expr, $str , $matches, PREG_PATTERN_ORDER ); header( 'Content-type: text/plain' ); // display in plain text print_r( $matches[1] ); ?> returns Array ( [0] => ae232asd [1] => 3231aadw ) It's slightly faster than positive look-ahead. It first searches for a space, or the start the string/new line It then checks for either letters followed by a number, or numbers followed by a letter It then captures the remaining letters or numbers Quote Link to comment https://forums.phpfreaks.com/topic/252802-how-to-match-a-part-number/#findComment-1296096 Share on other sites More sharing options...
imperium2335 Posted December 9, 2011 Author Share Posted December 9, 2011 What does the 's' do in the /si part? If I put this into my javascript it gives a syntax error: var partTest = /(?:^| )((?:[0-9]+[a-z]|[a-z]+[0-9])[a-z0-9]+)/si ; if I drop the 's' it works. Quote Link to comment https://forums.phpfreaks.com/topic/252802-how-to-match-a-part-number/#findComment-1296099 Share on other sites More sharing options...
Winstons Posted December 11, 2011 Share Posted December 11, 2011 What does the 's' do in the /si part? Modificator 's' - ignoring chars of new line "\r\n" or "\n". Modificator 'i' - ignoring the case of letters. JavaScript haven't the modifocator 's', instead of this use modif. 'g' Try this var partTest = /(?:^| )((?:\d+[a-z]|[a-z]+\d)[a-z\d]+)/gi; Quote Link to comment https://forums.phpfreaks.com/topic/252802-how-to-match-a-part-number/#findComment-1296743 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.