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";
    }
PwnageAtPwn
  • 431
  • 1
  • 6
  • 21
  • possible duplicate of [Converting ereg expressions to preg](http://stackoverflow.com/questions/6270004/converting-ereg-expressions-to-preg) – mario Dec 09 '11 at 22:23

9 Answers9

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • +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. – flesk Nov 28 '11 at 16:08
  • preg is "perl-compatible" and implements almost everything that you can do in perl regexes. – Marc B Nov 28 '11 at 16:09
  • 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. – flesk Nov 28 '11 at 16:13
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.

NullUserException
  • 83,810
  • 28
  • 209
  • 234
Jan Dragsbaek
  • 8,078
  • 2
  • 26
  • 46
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.

Wiseguy
  • 20,522
  • 8
  • 65
  • 81
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";
}
SERPRO
  • 10,015
  • 8
  • 46
  • 63
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)
Wiseguy
  • 20,522
  • 8
  • 65
  • 81
yunzen
  • 32,854
  • 11
  • 73
  • 106
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

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
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) {
NullUserException
  • 83,810
  • 28
  • 209
  • 234
  • Good point, though I figured the logical next step was to capture the ID code from that string with a subpattern. – Wiseguy Nov 28 '11 at 16:32
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.

Diblo Dk
  • 585
  • 10
  • 26
-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