0

The regex below takes the number (company id) out from the URL below, but I'm trying to find out how it works. What does hash to and pluses do?

preg_match('#/company/([0-9]+)/(.+)#',http://test.net.au/company/198/test,$m);
LSerni
  • 55,617
  • 10
  • 65
  • 107
John Kim
  • 1,842
  • 4
  • 37
  • 61
  • the `#` here is regexp boundaries, the `+` is the any number of character before `+` (>=1) – zb' Nov 22 '12 at 00:49
  • * See also [Open source RegexBuddy alternatives](http://stackoverflow.com/questions/89718/is-there) and [Online regex testing](http://stackoverflow.com/questions/32282/regex-testing) for some helpful tools, or [RegExp.info](http://regular-expressions.info/) for a nicer tutorial. And the PHP manual explains quite a bit too: http://php.net/manual/en/reference.pcre.pattern.syntax.php – mario Nov 22 '12 at 00:49

3 Answers3

1

Hash here is a regular expression delimiter. You can use almost anything as a delimiter, like ~, !, etc.

+ is a quantifier that means repetition from 1 to infinity.

[0-9]+ --- 1 or more numbers in a row
.+     --- any character one or more times in a row
zerkms
  • 249,484
  • 69
  • 436
  • 539
1

The first and last characters in the regex are delimiters. They are there just to say here is where the regex starts and here is where it stops:

Usually / is used:

Example: "/foo/";
         "#bar#";

In your example you are trying to match "/company/" so the delimiter can't be a / so # is used instead.

The plus + means match 1 or more of the previous entry. Meaning in your case it will match one or more digits (From 0 to 9)

Ibu
  • 42,752
  • 13
  • 76
  • 103
1

The hashes are just used as the regex pattern's bounding delimiters. Commonly developers use /, ~, or # for this. In this case # was used to prevent the need to escape the /'s in the regex pattern.

The + indicates that there must be one or more of the preceding element, which in the first case is actually a character class specifying all digits. In the second case it just means there need to be one or more of any character (. is wildcard).

Mike Brant
  • 70,514
  • 10
  • 99
  • 103