Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello I forgot where I found this at but I ran into these 3 examples which I'm very
confused about they produce the same output. Do these 3 examples mean the same thing?
What's the difference between all of them? And I am familiar with the last one but not the other two.

<?php
$string = 'I have 2 dogs.';
if(preg_match('~\d~',$string))
{
echo 'The string above contains a numeric character.';
}
else
{
echo 'The string above does not contain a numeric character.';
}
?>


<?php
$string = 'I have 2 dogs.';
if(preg_match('#\d#',$string))
{
echo 'The string above contains a numeric character.';
}
else
{
echo 'The string above does not contain a numeric character.';
}
?>


<?php
$string = 'I have 2 dogs.';
if(preg_match('/\d/',$string))
{
echo 'The string above contains a numeric character.';
}
else
{
echo 'The string above does not contain a numeric character.';
}
?>


What I have tried:

Google did not have what I was looking for so I thought I'll get a direct answer here.
Posted
Updated 14-Jan-18 0:23am

The chars at the start and the end are delimiters (preg_match requires them), and the pattern is inside the delimiters.

PHP: Delimiters - Manual[^]
Quote:
When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.

Often used delimiters are forward slashes (/), hash signs (#) and tildes (~).

So to answer your question "What's the difference?": the difference is only the delimiters, the patterns themselves have no difference.

But what's the purpose of allowing to choose a delimiter? That's also explained in the manual I linked above:
Quote:
If the delimiter needs to be matched inside the pattern it must be escaped using a backslash. If the delimiter appears often inside the pattern, it is a good idea to choose another delimiter in order to increase readability.
 
Share this answer
 
Quote:
Hello I forgot where I found this at but I ran into these 3 examples which I'm very confused about they produce the same output.

And that same output is?
If the output is 'The string above does not contain a numeric character.', it looks perfectly normal to me.
As far as I can see those characters are not RegEx control, they simply matched like any other chars.

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 
Comments
Member 13605567 13-Jan-18 18:22pm    
But don't you need to insert the expression between a opening and closing character like these with the forward slashes I thought I read some where that you need forward slashes to show that is where the regular expression code is located at. In other words the other examples don't have those forward slashes. So forward slashes aren't needed in regular expressions to symbolize that this section is where regular expression code constructed at?
Patrice T 13-Jan-18 18:42pm    
Each language handle RegEx in a different way. Some as a simple string, others with special chars, depends of the language.
Read documentation, look at samples.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900