Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/257182-regex-newbie/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1318339
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1318344
Share on other sites

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..  :P

 

I do appreciate you spotting it and linking the OP to an informative doc on the subject. Hopefully he/she will learn from it.

Link to comment
https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1318588
Share on other sites

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
}

Link to comment
https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1318642
Share on other sites

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..  :P

 

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.

Link to comment
https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1318651
Share on other sites

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..  :P

 

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.

Link to comment
https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1318659
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/257182-regex-newbie/#findComment-1319131
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.