1

Can someone help me with this bug. I need the following code to NOT match if there is another number afterwards

$query = "SELECT * FROM mytable WHERE server_name REGEXP '(server ?" . $server_id . ")' ";

For example, if $server_id is 50, it currently matches server 500, 501 etc and I dont want it too, but it should be allowed to match 'server50' 'server50 100mbit' 'server50,100mbit' etc. the character afterwards needs to be anything other than another number and can even be nothing at all.

Stu

2 Answers 2

1

Matching a non-digit is [^0-9] in regular expressions.

$query = "SELECT * FROM mytable 
    WHERE server_name REGEXP 'server ?" . $server_id . "([^0-9]|\$)' ";
Sign up to request clarification or add additional context in comments.

1 Comment

does that work if there is nothing at the end? e.g its just 'server50'?
1

You will want to do a regex against a number. /^0-9/ and what you would then need to do is to if returns a match, you need to say "Hey now, thats a number, wrong!" whereas if it returns an empty set, it would be a match.

You can also do a regex against it NOT being a number, where as you put the NOT character inbetween the ^ and the [.

I forget NOT syntaqx off the top of my head at the moment, but in common lingo, it would look like /^![0-9]/

the slashes are usually the sign of a regex.

with your stuff, you need to do something like /^serverid[0-9][0-9]/ which will compare the regex with SERVERID[][] so it will only read the first 2 characters. If there are MORE characers, you can boot it out, or things like that

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.