Jump to content

PHP Regular Expressions Best Tutorials web or books


j.smith1981

Recommended Posts

I have been trying to avoid learning Regular Expressions but feel I should really dig into them to be honest.

 

I just wanted to know if any of you know the best tutorials that are out there on the web, or even book guides on the various approaches to doing good regular expressions.

 

Just fancy buckling down and getting used to using them, want to make a username entry for a user sign up page for my project blog and fancied doing some of my own, just to teach myself really.

 

Any reply help is much appreciated,

Jeremy.

Link to comment
Share on other sites

Woops I forgot you had a tutorial here:

 

http://www.phpfreaks.com/tutorial/regular-expressions-part1---basic-syntax

 

Going to have a look at that, but are there any others should I get stuck that are good around on the web, or could you recommend any books on this particular aspect of logic?

 

But will for now read through the tutorial guides on this site,

 

Thank you and apologies.

Link to comment
Share on other sites

Thank you for that.

 

Will give that a read also, infact the one I posed the link I mean, was not that good.

 

But I know I can make them work for my own usage, which is why I am experimenting and it's making sense.

 

Just out of something completely random got something like this working:

 

<?php
$string = 777.'-';
$string .= 7777777;
$pattern = '/\d\d\d/'; // could start off with going along all the way its meant to be formatted, but that would be far too long!

 

I have managed to better this by doing the following:

<?php
$string = 777.'-';
$string .= 7777777;
$pattern = '/\d{3}-\d{7}/';
preg_match($pattern, $string, $matches);
print_r($matches);

 

That should output:

 

Array ( [0] => 777-7777777 )

 

Though that is just an off the top of my head experiment.

Link to comment
Share on other sites

When it says:

 

You can easily try the following yourself in a text editor that supports regular expressions, such as EditPad Pro

 

In this link:

http://www.regular-expressions.info/tutorial.html

 

Is Notepad++ a good regex editor at all, or does it have the capacity to do so?

 

If not is there any other free ones available I can use?

Link to comment
Share on other sites

Whilst on the hunt for a good free regexp editor, been playing around with various regex's.

 

It's actually sticking too!

 

Not being able to actually do very much except for literals but it's quite good now I am understanding somewhat what their actually doing rather than just thinking yes that would work but only relying purely on the author of a post, which I can not stand to do, if I start doing that I won't use it, even though it works, does anyone else do the same as me?

 

Anyways enough the waffle, just wanted to show one I have customised on a very simple level though:

 

http://www.webresourcesdepot.com/learn-test-regular-expressions-with-the-regulator/

 

I have then changed this slightly to:

preg_match('/H(á|ae?)nd(a|ae?)l/', $string, $matches);

 

Say I put in (as a literal):

'Hándael' it would find 3 occurences of the pattern in the string wouldn't it?

 

H is a literal? (please correct me if I am wrong the whole point of me making this post is so I can compare what I believe to be true as such).

 

ae is in the or with the | sign in brackets right?

then the final 'l' is a literal (sorry please correct me if I am using the wrong terminology for what I am explaining).

 

Would appreciate your feedback very much, just for memory what exactly is the metachar for anywhere in the string?

 

Just out of interest.

 

Alright I will leave it there, thank you and I appreciate any responses in advance,

Jeremy.

Link to comment
Share on other sites

 

I have then changed this slightly to:

preg_match('/H(á|ae?)nd(a|ae?)l/', $string, $matches);

 

Say I put in (as a literal):

'Hándael' it would find 3 occurences of the pattern in the string wouldn't it?

 

H is a literal? (please correct me if I am wrong the whole point of me making this post is so I can compare what I believe to be true as such).

 

ae is in the or with the | sign in brackets right?

then the final 'l' is a literal (sorry please correct me if I am using the wrong terminology for what I am explaining).

 

Would appreciate your feedback very much, just for memory what exactly is the metachar for anywhere in the string?

 

 

Right a literal, is "literally this character or string of characters."

 

As for matches, you are looking at it the wrong way, the pattern either matches or it doesn't.  There are not 3 occurrences of the pattern in the string you presented, there is one and it does match.

 

However, this also matches:  "Handal".

 

If that surprises you you want to look at the 2 "or" cases:

 

á|ae?

 

What that question mark after the e is saying is that the "prior character"  (the 'e') is optional when following an a.  So the pattern will match any of:

 

á
a
ae

 

The other thing to understand is the use of parens:  "()"

 

In regex when you put something inside parens, unless you add a character that tells the regex engine not to, the portion of inside the parens will be captured.  This is the key to doing search and replace, as each of these captured portions get numbered, and can then be used in substitutions.  So when you look at preg_replace for example, or even the array you get in $matches, it is based on the captured elements you have in your regex.  With that said, preg_match will either match a string or it won't.  It returns the number of matches it is able to find.  If you have no occurrence of the complete pattern you will not get a match.  In other words, just because I have:

 

"Hand" doesn't mean I'm going to get anything in $matches.

 

Check this out:

 


$string = 'Hand';
$matches = array();
$count = preg_match('/H(á|ae?)nd(a|ae?)l/', $string, $matches);

echo "$count Matches for $string\n";
var_dump($matches);


$string = "Handael";
$matches = array();
$count = preg_match('/H(á|ae?)nd(a|ae?)l/', $string, $matches);

echo "$count Matches for $string\n";
var_dump($matches);

 

Output:

 

0 Matches for Hand
array(0) {
}
1 Matches for Handael
array(3) {
  [0]=>
  string(7) "Handael"
  [1]=>
  string(1) "a"
  [2]=>
  string(2) "ae"
}

 

Notice that in $matches, when there is a complete match available, the "submatches"  get put into $matches.  The first element is the complete match found, and the next 2 elements are the submatches for the 2 sets of parens in your regex.

 

One last thing:  notepad++ does not have a full featured regex implementation. Many other editors do, but notepad++ is not one of them.

 

Link to comment
Share on other sites

One note on that Hándael example - it's a nice example for learning about regular expressions, but if you ever find yourself trying to match various ways someone might have written the same word using similar letters in Unicode, you'll usually be better off using the Intl class and learning ICU instead of trying to code out regular expressions to do it.

Link to comment
Share on other sites

Thank you ever so much for all your replies, really truly appreciate them!

 

I am learning so much about regex's now, thanks to your invaluable help.

 

I mean the whole point of me coming on here is to learn about programming area's, really appreciate your replies.

 

Will dig away around them, I mean with the unicode yes of course, but really all it was (and I think you realise this too) is that it was just entirely an example not really fussed about that for now, but will just dig into that if a case was to come up where I would need to do that and will go through the logic once again.

 

Of how many times you can search a string etc, I do understand though that I am kind of burrowing myself into a corner with these, always thinking of how to extend such expressions.

 

Like would it be best if I was to say only going to allow certain values into a regex, say like for a username validation, I was to just allow the following chars in them:

 

. (full stop or period)

_ (underscore)

0-9

a-z (if i wanted say A-Z, would I need to put the case insensitive metachar?)

 

I noticed when I tried putting in a regular expression, with literally [a-zA-Z] it allowed me to put in all kinds of characters, wanted to keep a fine control on what the user can input into them.

 

How would I allow all those quoted symbols anywhere and as many as they like in string? Might finally get me a slight push to get used to regex's.

 

Again really appreciate any replies,

Jeremy.

Link to comment
Share on other sites

I mean sorry would I list all the symbols or say if any other tag than these (the quoted ones in my previous post) are the only allowed valid ones?

 

How would I make the logic of a preg_match() take appreciation to this?

 

Just going through all the logic in the world, eventually will make sense but it's just I am not getting productional really on my own basis, could you help please?

 

I really fully appreciate the replies so far though!

 

Thank you for reading,

Jeremy.

Link to comment
Share on other sites

I think this thread has run its course.  For questions about a particular application of regex please make a new thread.  I have to admit that right now I don't understand what you are asking, so you might need to try and figure out a way to phrase the question that is clearer.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.