0

My current regular expression should be correct, though I wouldn't expect so, it doesn't work properly. It won't return "Got Match" My currrent code is as follows:

$id = "http://steamcommunity.com/id/TestID"; if (preg_match("^http://steamcommunity\.com/id/.*?\n$", $id)) { print "Got match!\n"; } 
1

9 Answers 9

4

You're missing delimiters on your regex:

if (preg_match("#^http://steamcommunity\.com/id/.*?\n$#", $id)) { ^--here ^--here 

Note that I've used # as the delimiter here, since that saves you having to escape all of the internal / charrs, if you'd used the traditional / as the delimiter.

Sign up to request clarification or add additional context in comments.

3 Comments

+1. I know that's possible in perl, and use it quite often, but I didn't know that it's possible in php too. I've hardly ever used php though. Regarding the question, there's also the newline he's trying to match against.
preg is "perl-compatible" and implements almost everything that you can do in perl regexes.
Yeah, but I've thought of the delimiters as more of language syntax than part of the regex. I see that it's not though, as it's often part of a regex string with optional flags after the closing delimiter.
3

You need a delimiter, like this:

if (preg_match("#^http://steamcommunity\.com/id/.*?$#", $id)) { ^ ^ 

And what's with the newline at the end? Surely you don't need that.

2 Comments

You've forgotten to escape the / inside the url, which is required if you use / as the delimiter.
i edited the post ten seconds ago to use a different delimiter
2

You're missing delimiters. For example:

"#^http://steamcommunity\.com/id/.*?\n$#" 

Also, you're trying to match a newline (\n) that isn't in your string.

Comments

2

You need to add the pattern delimiter:

$id = "http://steamcommunity.com/id/TestID"; if (preg_match("#^http://steamcommunity\.com/id/.*?(\n|$)#", $id)) { print "Got match!\n"; } 

Comments

1

There are a couple of things that are wrong with it. First of all, you need to delimit the start and end of your regex with a character. I used #. You're also matching for a new line at the end of your regex, which you don't have and likely won't ever have in your string.

<?php $id = "http://steamcommunity.com/id/TestID"; if (preg_match("#^http://steamcommunity\.com/id/.*?$#", $id)) { print "Got match!\n"; } ?> 

http://codepad.viper-7.com/L7XctT

Comments

1

First of all, your regex shouldn't even compile because it's missing delimiters.

if (preg_match("~^http://steamcommunity\.com/id/.*?\n$~", $id)) { ^---- these guys here -----^ 

Second of all, why do you have a \n if your string doesn't contain a new line?

And finally, why are you using regex at all? Effectively, you are just trying to match a constant string. This should be equivalent to what you are trying to match:

if (strpos($id, 'http://steamcommunity.com/id/') === 0) { 

1 Comment

Good point, though I figured the logical next step was to capture the ID code from that string with a subpattern.
1

You need to have starting and ending delimiter in your pattern like /pattern/ or #pattern# or with brackets (pattern). Why is that? To have some pattern modifiers after ending delimiter like #pattern#i (ignore case)

preg_match('(^http://steamcommunity\.com/id/.*?\n$)', $id) 

Comments

0

As the say your patten is start and end wrong. (Delimiter)
But this will be a better match of a 64-bit Steam ID. (Minimum 17 and Maximum 25 numbers)

if( preg_match("#^http://steamcommunity\.com/id/([0-9]{17,25})#i", $id, $matches) ) { echo "Got match! - ".$matches; } 

I believe that there is no need for you to require that the string must end with a line break.

Explanation.

http://steamcommunity\.com/id/([0-9]{17,25}) ^--- string ---^^-- Regexp --^ 

[0-9] - Match a number between 0 to 9
{17,25} - Make 17 to 25 matches
() - Returns match

Or use pattern as those (It is the same):

/^http:\/\/steamcommunity\.com\/id\/([0-9]{17,25})/i (^http://steamcommunity\.com/id/([0-9]{17,25}))i 

Regular Expressions PHP Tutorial
Online regular expression testing <- Dont use delimiter.

Comments

-2
<?php # URL that generated this code: # http://txt2re.com/index-php.php3?s=http://steamcommunity.com/id&-1 $txt='http://steamcommunity.com/id'; $re1='(http:\\/\\/steamcommunity\\.com\\/id)'; # HTTP URL 1 if ($c=preg_match_all ("/".$re1."/is", $txt, $matches)) { $httpurl1=$matches[1][0]; print "($httpurl1) \n"; } #----- # Paste the code into a new php file. Then in Unix: # $ php x.php #----- ?> 

Resorces: http://txt2re.com/index.php3?s=http://steamcommunity.com/id&-1

1 Comment

Why is there a perl answer to a php question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.