PDA

View Full Version : PCRE... and i thought i understood it


silentace
07-28-2008, 05:32 PM
Ok so here are a few examples of what i am trying to allow

2E2X1
3C051
5B174

First spot needs to be a number, second a letter, third a number again, forth any of the following: X,1,3,5,7,9 and then fifth a number again.

I am also trying to allow it to have "DEP" and "Civ" as an option to type as well but would like the capitalization to hold. So here is my expression but i don't fully understand it as much as i thought i did

^[0-9][A-Z][0-9][X13579][0-9]|[DEP]|[Civ]$

Any help would be much appreciated.

Marco van Herwaarden
07-29-2008, 05:49 AM
Something like:

^([0-9][A-Z][0-9][X13579][0-9])|DEP|Civ$

Your biggest error seems to be that you have square brackets around the "DEP" and "Civ", this tells that it shoud have 1 of the characters D, E or P (and C, i, v for the 2nd), instead of matching the word "DEP"

silentace
07-29-2008, 03:57 PM
ok so with that it allows me to put depP and civV... the extra characters and wrong capitals (although i guess nit picky of me) are not what i am trying to do. With [A-Z] it doesn't allow lower case... how can i do that for DEP and Civ?

Dismounted
07-30-2008, 06:53 AM
As long as you aren't using the "i" modifier, it should be case sensitive.

silentace
07-30-2008, 03:11 PM
As long as you aren't using the "i" modifier, it should be case sensitive.

I am using that identical PCRE expression above and i can put more characters then just DEP and casing isn't being followed

Dismounted
07-31-2008, 06:59 AM
/^([0-9][A-Z][0-9][X13579][0-9])(DEP|Civ)$/

silentace
07-31-2008, 04:47 PM
/^([0-9][A-Z][0-9][X13579][0-9])(DEP|Civ)$/

I wish I understood this better... that doesn't work either. Doesn't allow for any combo of lower/upper letters with dep or civ... it also doesn't allow for any combo of the first sequence either. no 2E251 or anything.

MoT3rror
07-31-2008, 07:05 PM
Try ^([0-9][A-Z][0-9][X13579][0-9])|(DEP|Civ)$

Also I use a program called Regex Coach (http://www.weitz.de/regex-coach/) to test things like this before I use it in PHP.

silentace
07-31-2008, 08:01 PM
Try ^([0-9][A-Z][0-9][X13579][0-9])|(DEP|Civ)$

Also I use a program called Regex Coach (http://www.weitz.de/regex-coach/) to test things like this before I use it in PHP.

I think there is no hope for me... not working either. I had tried regex coach i will have to try it again

Dismounted
08-01-2008, 06:18 AM
/^([0-9][A-Z][0-9][X13579][0-9])(DEP|Civ){0,1}$/

silentace
08-01-2008, 01:05 PM
/^([0-9][A-Z][0-9][X13579][0-9])(DEP|Civ){0,1}$/

Closer but still no cigar. BTW thank you guys for all your help. That string allows me to put in the first type like 2E2X1 and stops lower case letters but it won't allow any combo of dep or civ no matter what case. Again thanks for the help and if anyone has more ideas I welcome anything right now =)

Dismounted
08-02-2008, 04:41 AM
My code (post #10) does work - I have tested it myself. Please post your implementation of it.

silentace
08-02-2008, 05:26 AM
My code (post #10) does work - I have tested it myself. Please post your implementation of it.

I am not sure what posting my implementation means? do you want the site i run it on? or do you want me to copy and paste exactly what i put in?

^([0-9][A-Z][0-9][X13579][0-9])(DEP|Civ){0,1}$

With Regex Coach it doesn't work with dep/DEP/civ/Civ or anything. I also tried on my site and it'not working either, same issue as with regex. I even tried with the beginning and trailing "/" but that didn't work either.

Digital Jedi
08-02-2008, 05:43 AM
Isn't there supposed to be a pipe in there?

^([0-9][A-Z][0-9][X13579][0-9])|(DEP|Civ){0,1}$

silentace
08-02-2008, 07:04 AM
Isn't there supposed to be a pipe in there?

^([0-9][A-Z][0-9][X13579][0-9])|(DEP|Civ){0,1}$

With the last few ideas i have tried throwing it in there and it doesn't help

Dismounted
08-02-2008, 11:07 AM
I am not sure what posting my implementation means? do you want the site i run it on? or do you want me to copy and paste exactly what i put in?

^([0-9][A-Z][0-9][X13579][0-9])(DEP|Civ){0,1}$

With Regex Coach it doesn't work with dep/DEP/civ/Civ or anything. I also tried on my site and it'not working either, same issue as with regex. I even tried with the beginning and trailing "/" but that didn't work either.
Implementation = how you are using it (ie. post the code that is using the regex).
Isn't there supposed to be a pipe in there?

^([0-9][A-Z][0-9][X13579][0-9])|(DEP|Civ){0,1}$
No, a pipe will not work - furthermore, my condition at the end makes it optional "{min,max}".

Digital Jedi
08-02-2008, 04:11 PM
Reason I asked is because I was testing it in the Regulator and I couldn't get a match until I added the pipe.

MoT3rror
08-02-2008, 04:49 PM
http://www.combatarenas.com/test.php

Then here is the php code.

<?php
echo 'Checking /^([0-9][A-Z][0-9][X13579][0-9])|(DEP|Civ){0,1}$/<br />';

$values = array('2E2X1', '3C051', '5B174', 'DEP', 'Civ');

foreach($values AS $value)
{
if(preg_match('/^([0-9][A-Z][0-9][X13579][0-9])|(DEP|Civ){0,1}$/', $value))
{
echo $value . ' succeed<br />';
}
else
{
echo $value . ' failed<br />';
}
}

echo '<br /><br />Checking /^([0-9][A-Z][0-9][X13579][0-9])(DEP|Civ){0,1}$/<br />';


foreach($values AS $value)
{
if(preg_match('/^([0-9][A-Z][0-9][X13579][0-9])(DEP|Civ){0,1}$/', $value))
{
echo $value . ' succeed<br />';
}
else
{
echo $value . ' failed<br />';
}
}
?>


So there is aleast one that is working.

silentace
08-02-2008, 07:57 PM
I guess either the software that tests the code or vbulletin/regex doesn't interpret the equation the right now. in the software it shows

^([0-9][A-Z][0-9][X13579][0-9])|(DEP|Civ){0,1}$

as working perfectly for what i want. DEP is a match Dep is not. Civ is a match and CIV is not. But that exact same code on vbulletin not only matches DEP but dep and depp... and so on. Its like vbulletin doesn't even use the equation that i am putting in.

Dismounted
08-03-2008, 04:26 AM
Whoops. I thought you wanted to allow "DEP" or "Civ" tacked onto the end of the string. Sorry, my bad. :o

This one should work.
/^(([0-9][A-Z][0-9][X13579][0-9])|(DEP|Civ))$/

silentace
08-03-2008, 08:53 AM
Whoops. I thought you wanted to allow "DEP" or "Civ" tacked onto the end of the string. Sorry, my bad. :o

This one should work.
/^(([0-9][A-Z][0-9][X13579][0-9])|(DEP|Civ))$/

My fault for not being so clear... is there any way that vB isn't translating it right? Regex Coach shows that works but vB still allows for lower case/upper case mixtures for DEP and Civ...

It doesn't however allow lower case like 2e2x1 so that is working right, but it allows dep and cIV

Dismounted
08-03-2008, 10:15 AM
How are you "using" it in vBulletin?

silentace
08-03-2008, 12:36 PM
yes, i only test it in regex coach after its not working in vbulletin to make sure i'm not crazy. but i get mixed results between the two.

one question... you always post it with a pre and post "/"

is that suppose to be in there? i always just use the ^ to the $

Dismounted
08-03-2008, 12:38 PM
Please read my post again :).
How are you "using" it in vBulletin?

silentace
08-03-2008, 01:56 PM
I have a custom profile field and then i have it displaying in posbits under username/title. It is a 5 character limited text field.

http://img257.imageshack.us/img257/6791/33592547qi5.th.jpg (http://img257.imageshack.us/img257/6791/33592547qi5.jpg)

thumbnail isn't linking right so here is full image
http://img257.imageshack.us/img257/6791/33592547qi5.jpg

Marco van Herwaarden
08-04-2008, 07:45 AM
vBulletin by default adds the following modifiers: '#siU'

So it will always ignore case. Not enough of a PCRE expert myself to know how to force some words to case-sensitive again.

Dismounted
08-04-2008, 12:05 PM
^(?-i)(([0-9][A-Z][0-9][X13579][0-9])|(DEP|Civ))$

silentace
08-04-2008, 02:49 PM
i think we have success... you rock dismounted!

thanks for everyones help

so what did this do? "(?-i)"

Dismounted
08-05-2008, 12:00 PM
Turns the following parenthesised statement to case sensitive, regardless of the modifiers. "(?i)" does the opposite.