Questions tagged [preg-match]

Regular expression pattern matching function for the PHP programming language

The preg_match() function in searches for a match to the regular expression given using Perl Compatible Regular Expression (PCRE) syntax. Specifically, it is for matching one value or one set of data with a given pattern.

If you require multiple matches that match your pattern, you will want to use the function

Description

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )

Basic matching

preg_match('/PHP/', 'PHP')       # Match for an unbound literal
preg_match('/^PHP/', 'PHP')      # Match literal at start of string
preg_match('/PHP$/', 'PHP')      # Match literal at end of string
preg_match('/^PHP$/', 'PHP')     # Match for exact string content
preg_match('/^$/', '')           # Match empty string

Using different regular expression delimiters

preg_match('/PHP/', 'PHP')       # / as commonly used delimiter
preg_match('@PHP@', 'PHP')       # @ as delimiter
preg_match('!PHP!', 'PHP')       # ! as delimiter 

References

5363 questions
186
votes
4 answers

Regex: Specify "space or start of string" and "space or end of string"

Imagine you are trying to pattern match "stackoverflow". You want the following: this is stackoverflow and it rocks [MATCH] stackoverflow is the best [MATCH] i love stackoverflow [MATCH] typostackoverflow rules [NO MATCH] i love…
anonymous-one
  • 14,454
  • 18
  • 60
  • 84
151
votes
4 answers

How can I convert ereg expressions to preg in PHP?

Since POSIX regular expressions (ereg) are deprecated since PHP 5.3.0, I'd like to know an easy way to convert the old expressions to PCRE (Perl Compatible Regular Expressions) (preg). Per example, I have this regular expression: eregi('^hello…
netcoder
  • 66,435
  • 19
  • 125
  • 142
137
votes
2 answers

PHP regular expressions: No ending delimiter '^' found in

I've been having some trouble with regular expressions. This is my code $pattern = "^([0-9]+)$"; if (preg_match($pattern, $input)) echo "yes"; else echo "nope"; I run it and get: Warning: preg_match() [function.preg-match]: No ending…
fingerman
  • 2,440
  • 4
  • 19
  • 24
128
votes
1 answer

"Unknown modifier 'g' in..." when using preg_match in PHP?

This is the regex I'm trying to use: /^(\w|\.|-)+?@(\w|-)+?\.\w{2,4}($|\.\w{2,4})$/gim I found it on this site, and it works great when I try it out there. But as soon as I place it in my code, I get this message: Warning: preg_match()…
Nike
  • 1,479
  • 3
  • 10
  • 9
119
votes
3 answers

Regular expression containing one word or another

I need to create an expression matching a whole number followed by either "seconds" or "minutes" I tried this expression: ([0-9]+)\s+(\bseconds\b)|(\bminutes\b) It works fine for seconds, but not minutes. E.g. "5 seconds" gives 5;seconds; while "5…
user965748
  • 2,227
  • 4
  • 22
  • 30
110
votes
7 answers

Delimiter must not be alphanumeric or backslash and preg_match

I have this code : $string1 = "My name is 'Kate' and im fine"; $pattern = "My name is '(.*)' and im fine"; preg_match($pattern , $string1, $matches); echo $matches[1]; and when im run it returns this error: Warning: preg_match()…
user547363
  • 1,189
  • 2
  • 8
  • 7
77
votes
13 answers

Split camelCase word into words with php preg_match (Regular Expression)

How would I go about splitting the word: oneTwoThreeFour into an array so that I can get: one Two Three Four with preg_match ? I tired this but it just gives the whole word $words =…
CodeChap
  • 4,132
  • 6
  • 30
  • 40
76
votes
5 answers

preg_match(): Compilation failed: invalid range in character class at offset

Thank you in advance for you time in helping with this issue.. preg_match(): Compilation failed: invalid range in character class at offset 20 session.php on line 278 This stopped working all of a sudden after months of working, after a PHP…
user3841888
  • 771
  • 1
  • 5
  • 3
70
votes
1 answer

Optional Whitespace Regex

I'm having a problem trying to ignore whitespace in-between certain characters. I've been Googling around for a few days and can't seem to find the right solution. Here's my code: // Get Image data preg_match('#
jameslfc19
  • 1,084
  • 1
  • 10
  • 14
66
votes
6 answers

How to search in an array with preg_match?

How do I search in an array with preg_match? Example: name', 'this') , $match) ) { //Excelent!! $items[] = $match[1]; } else { //Ups! not found! } ?>
Olaf Erlandsen
  • 5,817
  • 9
  • 41
  • 73
54
votes
5 answers

PHP - regex to allow letters and numbers only

I have tried: preg_match("/^[a-zA-Z0-9]", $value) but im doing something wrong i guess.
Jason94
  • 13,320
  • 37
  • 106
  • 184
52
votes
3 answers

PHP's preg_match() and preg_match_all() functions

What do the preg_match() and preg_match_all() functions do and how can I use them?
sikas
  • 5,435
  • 28
  • 75
  • 120
47
votes
3 answers

Regex to match an IP address

I am newbie with regex and I want to use preg_match function to find if a string is an IP address. For example, $string = "10.0.0.1"; preg_match($regex, $string); should return true. So, what $regex should be?
ibrahim
  • 3,254
  • 7
  • 42
  • 56
46
votes
12 answers

PHP is_numeric or preg_match 0-9 validation

This isn't a big issue for me (as far as I'm aware), it's more of something that's interested me. But what is the main difference, if any, of using is_numeric over preg_match (or vice versa) to validate user input values. Example One:
no.
  • 2,356
  • 3
  • 27
  • 42
44
votes
4 answers

preg_replace how to replace only matching xxx($1)yyy pattern inside the selector

I'm trying to use a regular expression to erase only the matching part of an string. I'm using the preg_replace function and have tried to delete the matching text by putting parentheses around the matching portion.…
PartySoft
  • 2,749
  • 7
  • 39
  • 55
1
2 3
99 100