PDA

View Full Version : Need help with ereg


JJR512
02-18-2002, 07:52 AM
When you're using the ereg() expression, and you have multiple items inside it, such as this:
ereg('^(M?)([0-9]{1,2})(/?)([0-9]{1})?SM$',$group,$regs)
Does it matter if the ? symbols go inside or outside the ()s? It looks to me like sometimes it does matter, sometimes it doesn't. That sample line was giving me a lot of trouble in the form of this error message: "Warning: REG_BADRPT in /home/jjr512/public_html/forums/xxxx.php on line 40". Originally, I had the last ? (the one right before the SM) just inside the ) that's now right before it. When I moved it to just outside that ), which is how you see it now, the problem went away. But there are still two instances of a ? inside some ()s earlier on that line, and that doesn't seem to be a problem; why is that?

Admin
02-18-2002, 08:01 AM
That's because you had something like this:
[0-9]{1}?
{1} means you only want one digit, right? But "?" means you want it to appear 0 or 1 times. That's not right, because you have two quantifiers on the same expression ([0-9]).
By putting ? outside the parentheses, you are saying you want the whole thing (a digit that appears exactly once) to appear 0 or 1 times, which is better (but still pointless ;)).

There is no need in putting {1} because that has no affect on the outcome of the regex.

Also you should use preg_match(), it's faster. :)

JJR512
02-18-2002, 05:52 PM
So I could either use ([0-9])? or ([0-9]{0,1}) and either is the exact same thing, correct?

Also, as to the preg_match, do I just replace all instances of "ereg" with it, or do I need to recode anything (in other words, is it the exact same syntax)?

Admin
02-19-2002, 11:05 AM
No, not exactly the same. You need to add delimiters before and after the expression. Most use / as the delimiter but I prefer # as it's less likely to appear in the expression itself (because if / is in it, you must escape it like \/).

The parentheses matter, don't get me wrong. The matches you will have in the $regs array will only contain what's between them. So if you put something out of the parentheses, you won't get it in the matching array.

JJR512
02-19-2002, 05:20 PM
So what would be the best way to write an expression that looks for either a single digit or no digit?

Looking at my original expression,
ereg('^(M?)([0-9]{1,2})(/?)([0-9]{1})?SM$',$group,$regs)
What I really want is to find any of the following:
M1/1SM
M1SM
M11SM
1/1SM
1SM
11SM
(where '1' represents any single-digit number, all other characters are literal)

Any of those substrings will be surrounded by a blank space on both sides.

SM will always appear in it, and will always be preceded by at least one digit, possibly two. If it's preceded by one digit, that one digit may or may not be preceded by an M. If that one digit is not preceded by an M, it may or may not be preceded by a forward slash; if it is, it must be preceded by another single-digit number, and that single digit may or may not be preceded by an M. If the SM is preceded by two digits, those two digits may be preceded by an M, and that's it. And how I need all that grouped into something I can work with (by putting it into $regs[x]) is that each element should be separate, unless it's two digits together, because that is one number. Also, if there is a forward slash, the single digit before it and the single digit after it, and the slash, can all go together, although I think it would be easier to smash them together later (that is a fraction).