Question

I'm having trouble constructing this regex. I need a strpos or a preg_match that will match the following pattern: 00x00. where the '0's are any number(s), and x is upper or lowercase, then a period.

I know I can do \d for numbers, but don't know how to specify two or more numbers. I know I can do [xX] for either upper or lowercase 'x', and I'm lost on the period.

I've tried this:

preg_match ( '/\\d[xX]\\d\./', $string )

but that isn't working.

I'd prefer to do it with a strpos, but either is fine.

EDIT - The period is optional; it's just added security. But I'm not sure if it's possible to match it not knowing how many numbers are between the 'x' and the period.

Was it helpful?

Solution

Something like so: \d{2,}[Xx]\d{2,}\. will match two or more digits, an x (ignoring case) and two more digits and a period.

If that is all you want to match, I would recommend adding the anchor tags: ^\d{2,}[Xx]\d{2,}\.$. This will instruct the regex engine to match exactly that.

So in short, this \d{2,}[Xx]\d{2,}\. will match foo bar 000X00 foo bar but this: `^\d{2,}[Xx]\d{2,}\.$ will match only 000X00 and nothing else (so it will fail for my previous example).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top