lollipop2468 Posted April 23, 2012 Share Posted April 23, 2012 Apologies for the ridiculousness of this question! I am a newbie so please be patient! I am trying to match groups of numbers and letters, however how do I match a possible space in an expression? By this I mean if a user entered 'ABCD 1234' how can I get it to match with ABCD1234? This is most likely a very simple piece of code - ive searched all afternoon and can not find anything to explain how it is done! Thanks Quote Link to comment Share on other sites More sharing options...
requinix Posted April 23, 2012 Share Posted April 23, 2012 The simplest way would be (space)? like /^[A-Z]{4} ?[0-9]{4}$/ but there are a few other ways. It depends on what your expression is. By the way, what is it? Quote Link to comment Share on other sites More sharing options...
lollipop2468 Posted April 23, 2012 Author Share Posted April 23, 2012 Thanks requinix! Something so simple! I'm trying to learn the basics and struggling to find a guide that explains it easily! So will ' ?' check for either space or no space? If a line was approved as either having/not having a space, is it then possible to strip out the space if you were then going to insert that line into a database? Quote Link to comment Share on other sites More sharing options...
xyph Posted April 23, 2012 Share Posted April 23, 2012 ? matches 0 or 1 instances of the previous character/class/group. Quote Link to comment Share on other sites More sharing options...
requinix Posted April 23, 2012 Share Posted April 23, 2012 If a line was approved as either having/not having a space, is it then possible to strip out the space if you were then going to insert that line into a database? Sure. One way would be to "capture" the letters and numbers, then piece the two parts back together. I felt like drawing some ASCII art. Probably because I'm really tired. "ABCD 1234" / | \ / | \ V V V ______ ______ / \ / \ | | | | /^([A-Z]{4}) ?([0-9]{4})$/ | | | | \________/ \________/ \ / $1 \ / $2 \ / V V "$1$2" | V "ABCD1234" Quote Link to comment 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.