PCRE Regex Syntax -


i guess more or less two-part question, here's basics first: writing php use preg_match_all in variable strings book-ended {}. iterates through each string returned, replaces strings found data mysql query.

the first question this: sites out there learn ins , outs of pcre expressions? i've done lot of searching on google, best 1 i've been able find far http://www.regular-expressions.info/. in opinion, information there not well-organized , since i'd rather not hung having ask whenever need write complex regex, please point me @ couple sites (or couple books!) me not have bother folks in future.

the second question this: have regex

"/{.*(_){1}(.*(_){1}[a-z]{1}|.*)}/" 

and need catch instances such {first_name}, {last_name}, {email}, etc. have 3 problems regex.

the first sees "{first_name} {last_name}" 1 string, when should see two. i've been able solve checking existence of space, exploding on space. messy, works.

the second problem includes punctuation part of captured string. so, if have "{first_name} {last_name},", returns comma part of string. i've been able partially solve using preg_replace delete periods, commas, , semi-colons. while works punctuation items, logic unable handle exclamation points, question marks, , else.

the third problem have regex is not seeing instances of {email} @ all.

now, if can, willing, , have time hand me solution problem, thank solve immediate problem. however, if can this, please please provide lmgfty provides web sites references and/or book or 2 provide education on subject. sites preferable money tight, if book solution, i'll find money (assuming local library system unable procure said volume).

back found php's own pcre syntax reference quite good: http://uk.php.net/manual/en/reference.pcre.pattern.syntax.php

let's talk expression. it's quite bit more verbose necessary; i'm going simplify while go through this.

a rather simpler way of looking @ you're trying match: "find {, number of letters or underscores, }". regular expression (in php's string-y syntax): '/\{[a-z_]+\}/'

this match of examples wilder ones {__a_b}. if that's not option, can go more complex description: "find {, bunch of letters, (as possible) underscore followed bunch of letters, }". in regular expression: /\{([a-z]+(_[a-z]+)*\}/

this second 1 maybe needs bit more explanation. since want repeat thing matches _foo segments, need put in parentheses. say: try finding possible, it's okay if don't find @ (that's meaning of *).

so have compare attempt to, let's have @ caused problems:

  • your expression matches characters inside {}, including } , { , whole bunch of other things. in other words, {abcde{_fgh} accepted regex, {abcde} fg_h {ijkl}.
  • you've got mandatory _ in there, right after first .*. (_){1} (which means same _) says: whatever happens, explode if ain't here! don't want that, because it'll never match {email}.

here's complete description in plain language of regex matches:

  1. match {.
  2. match _.
  3. match absolutely long can match remaining rules right after anything.
  4. match _.
  5. match single letter.
  6. instead of _ , single letter, absolutely okay, too.
  7. match }.

this pretty far wanted. don't worry, though. regular expressions take while used to. think it's helpful if think of in terms of instructions, i.e. when building regular expression, try build in head "find this, find that", etc. figure out right syntax achieve that.

this hard because not instructions might come in head translate piece of regular expression... that's experience comes in. promise you'll have down in no time @ all... if methodical making regular expressions @ first.

good luck! :)


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -