Solved

point in regular expressions php

Posted on 2014-04-15
10
185 Views
Last Modified: 2014-04-15
Hi,

I have the following code

preg_match_all('.u.',"Fuse",$matches);
print_r ($matches);

Open in new window


Why it not return the word  "use" ?
0
Comment
Question by:joyacv2
  • 5
  • 5
10 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 40002464
It should return 'Fus'.

And you forgot the delimiters:

preg_match_all('/.u./',"Fuse",$matches);

You're searching for:
any character
u
any character

HTH,
Dan
0
 
LVL 1

Author Comment

by:joyacv2
ID: 40002471
yes, you are right i want to say Fus, the script returns

Array ( [0] => Array ( [0] => us ) )

Why don't return Fus?
0
 
LVL 1

Author Comment

by:joyacv2
ID: 40002475
Hi Dan, why I have to use /?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40002476
Put the delimiters and the result will be:
Array ( [0] => Array ( [0] => Fus ) )
0
 
LVL 1

Author Comment

by:joyacv2
ID: 40002481
now works, what is the purpose of the /?
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40002488
It's the custom in PHP. You don't have to use //, you can use %%, !! etc

I guess the reason is that you can specify the modifiers/flags after the last delimiter (for ex /expr/i will make the search case insensitive).

It's the way sed/awk use regular expressions and probably someone thought it would be easier to keep that format.

http://us.php.net/manual/en/pcre.examples.php
http://us.php.net/manual/en/regexp.reference.delimiters.php
0
 
LVL 1

Author Comment

by:joyacv2
ID: 40002498
sorry, but i am a little confuse, i am reading a book for regular expressions and not mention anything about adding a / before .

Can you explain me a little bit about that?
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40002501
Most languages do not require delimiters, but PHP does.

Read this: http://us.php.net/manual/en/regexp.reference.delimiters.php
0
 
LVL 1

Author Closing Comment

by:joyacv2
ID: 40002504
perfect!!!
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40002508
BTW, I recommend this site: http://www.regular-expressions.info/tutorial.html

I used it a lot while learning.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question