By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,499 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

idiot question about preg_match and regex

P: n/a
I apologize for my igorance of Regex. I can't get this function to
work. It gives the following error message.

function checkForFloatingPoint($input=false) {
$pattern = "[-+]?([0-9]*\.[0-9]+|[0-9]+)";
$match = preg_match (pattern, subject);
echo "The match is: $match ";
return $match;
}


Warning: preg_match(): Delimiter must not be alphanumeric or backslash
in /home/hoovera/public_html/lawrenceScripts/lk_lib.php on line 642
The match is:

Oct 27 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

I know this is bad technique, but if I want to say "any 2 digits
followed by a decimal followed by any 5 more digits" then I could say
this, yes?

[0-9][0-9]\.[0-9][0-9][0-9][0-9][0-9]

Oct 27 '05 #2

P: n/a
On 27/10/2005 14:28, lk******@geocities.com wrote:

[snip]
function checkForFloatingPoint($input=false) {
I don't really see how boolean false is a useful default value, or even
why you'd want a default argument for the input.

What is this function supposed to do? Exactly what pattern(s) are you
trying to match or test for? It's identifier suggests that it should
look for some aspect of a floating-point number (though it's not clear
what), yet your pattern allows integers as well.
$pattern = "[-+]?([0-9]*\.[0-9]+|[0-9]+)";
You haven't included any delimiters. A forward slash, at both the start
and end of the pattern, is common.
$match = preg_match (pattern, subject);


You haven't included the dollar ($) symbol before each identifier.

[snip]

Mike
Don't use tabs to indent. Tabs are usually rendered with the equivalent
of eight spaces, which is far too much when content is usually wrapped
at eighty characters, or less. Use spaces; preferably two.

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Oct 27 '05 #3

P: n/a
just change
$pattern "/[-+]?([0-9]*\.[0-9]+|[0-9]+)/";

see the slashes?

Oct 27 '05 #4

P: n/a
[0-9]{2}\.[0-9]{5}
looks better

Oct 27 '05 #5

P: n/a
>What is this function supposed to do? Exactly what pattern(s) are you
trying to match or test for? It's identifier suggests that it should
look for some aspect of a floating-point number (though it's not clear
what), yet your pattern allows integers as well.


Thank you for help. I apologize for asking such a poor question. I've a
simple one to follow up with: how do you say "not" in regex? Is it "^"?

I've decided that all I want to do is make sure there are no letters in
the input. Is it right to say this:

$pattern "[^A-Za-z]";
$match = preg_match ($pattern, $subject);

This comes back true if there are no letters in $subject, yes?

Oct 28 '05 #6

P: n/a
lk******@geocities.com wrote:
how do you say "not" in regex? Is it "^"?
Yes and no. http://php.net/manual/en/reference.p...ern.syntax.php
I've decided that all I want to do is make sure there are no letters in
the input. Is it right to say this:

$pattern "[^A-Za-z]";


That is not a pattern because it doesn't have pattern delimiters. It also
doesn't have any quantifiers (+, * or {}), or start- or end-of-subject
pinning (^ and $). And you can use a pattern modifier for case-insensitive
matching, see
http://php.net/manual/en/reference.p....modifiers.php

--
E. Dronkert
Oct 28 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.