Take the 2-minute tour ×

im kinda strumped in a situation where i need to match a whole string with a regular expression rather than finding if the pattern exists in the string.

suppose if i have a regular expression


what i want is that the code will run through various strings , compare the strings with the regular expression and perform some task if the strings start and end with a ^.


^hello world^ is a match

my ^hello world^ should not be a match

the php function preg_match matches both of the results

any clues ???

share|improve this question
Just to clarify, even though there are a number of answers already, does the text between the "^"s need to be only one word? Can it contains spaces or other punctuation? Can it contain "^"s? More details would allow for a more concrete regex. –  Will Nov 30 '10 at 5:27
I've edited my answer, do try –  SiGanteng Dec 3 '10 at 7:54

6 Answers 6

up vote 3 down vote accepted

Here is a way to do the job:

$strs = array('^hello world^', 'my ^hello world^');
foreach($strs as $str) {
    echo $str, preg_match('/^\^.*\^$/', $str) ? "\tmatch\n" : "\tdoesn't match\n";


^hello world^   match
my ^hello world^        doesn't match
share|improve this answer
+1 for actually putting the regex into a code example. –  Will Dec 1 '10 at 18:20
Also, always try to use a single-quoted string for the regex as M42 did, to avoid leaning-toothpick syndrome (i.e. too many backslashes) and the extra processing that double-quoted strings are subject to. –  Alan Moore Dec 3 '10 at 10:28

Anchor the ends.

share|improve this answer

Actually, ^\^\w+\^$ will not match "^hello world^" because you have two words there; the regex is only looking for a single word enclosed by "^"s.

What you are looking for is: ^\^.*\^$ This will match "^^", "^hello world^", "^a very long string of characters^", etc. while not matching "hello ^world^".

share|improve this answer

You can use the regex:


^ is a regex meta-character which is used as start anchor. To match a literal ^ you need to escape it as \^.

So we have:

  • ^ : Start anchor
  • \^: A literal ^
  • [\w\s]+ : space separated words.
  • \^: A literal ^
  • $ : End anchor.

Ideone Link

share|improve this answer
good explanation, but as I mentioned, the \w+ only matches a single word. If there are multiple words (that is, more than one word, each separated by whitespace), the pattern will not match. There are various ways to do it, but the simplest and most complete match would be to use .* which will match 0 or more characters, regardless of what they are. –  Will Nov 30 '10 at 4:17
not trying to be a nit-pick, but [\w\s]+ places a requirement that there be at least one word character or whitespace character between the "^"s. So it would not match "^^" or "^123^" (or does \w match digits as well? going to have to check that). I commented to the OP for more clarification; I think we are all making some assumptions as to the specifics of the matched pattern. –  Will Nov 30 '10 at 5:33
@Will: Yes, \w matches digits as well as letters. What that regex misses is punctuation characters. –  Alan Moore Dec 3 '10 at 10:41

Another pattern is: ^\^[^\^]*\^$ if you want match "^hello world^" and not "hello ^world^" , while \^[^\^]*\^ if you want match "^hello world^" and world in the "hello ^world^" string.

For Will: ^\^.*\^$ this match also "^hello^wo^rld^" i think isn't correct.

share|improve this answer
If I read the OP correctly, I believe the only requirement was that there be "^"s at the beginning and ending of the string; as far as having them in the string as well was not mentioned. "my ^hello world^" should not match because it does not start with "^", not because it has "^" somewhere in the middle of the string. –  Will Nov 30 '10 at 5:23


share|improve this answer

Your Answer


