[PHP] Simple RegEx to pull out content between 2 markers
Hi, I need to grab the what's between 2 markers in a Textile / html string, but my regex skills aren't all that hot. The text I have is: OPEN_MAIN_COLUMN h3. Article Content Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. p(image). !/app/image/1/large! Caption p(image). !/app/image/24/large! drunken jonty CLOSE_MAIN_COLUMN OPEN_LEFT_COLUMN h3. Article Content !/app/image/7/thumb! Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. CLOSE_LEFT_COLUMN OPEN_RIGHT_COLUMN h3. Article Content Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. * Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. * Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. * Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. CLOSE_RIGHT_COLUMN I'd like to grab the contents of left, right and main individually so I can place them in separate form fields. Any help greatly appreciated. I'm certain this is a cinch for people who use regex often. Thanks, Jon -- jon bennett w: http://www.jben.net/ iChat (AIM): jbendotnet Skype: jon-bennett -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple RegEx to pull out content between 2 markers
2008. 03. 29, szombat keltezéssel 11.26-kor Jon Bennett ezt írta: Hi, I need to grab the what's between 2 markers in a Textile / html string, but my regex skills aren't all that hot. The text I have is: OPEN_MAIN_COLUMN h3. Article Content Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. p(image). !/app/image/1/large! Caption p(image). !/app/image/24/large! drunken jonty CLOSE_MAIN_COLUMN OPEN_LEFT_COLUMN h3. Article Content !/app/image/7/thumb! Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. CLOSE_LEFT_COLUMN OPEN_RIGHT_COLUMN h3. Article Content Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. * Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. * Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. * Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. CLOSE_RIGHT_COLUMN I'd like to grab the contents of left, right and main individually so I can place them in separate form fields. Any help greatly appreciated. I'm certain this is a cinch for people who use regex often. preg_match('/OPEN_MAIN_COLUMN(.*)CLOSE_MAIN_COLUMN/sUi', $string, $matches); echo $matches[1]; it's not very complicated, the only interesting bits are the s and U modifiers at the end of the pattern. s makes the dot (.) to match newlines too, so the matched text can be more than one line, the U makes the engine 'ungreedy' which means that it stops after the first match. this means that if you have a string like opencloseopenclose and match for open(.*)close, without the U modifier you would get the part between the first open and the last close. with the modifier you get the parts between any open and the next close more info here: http://hu.php.net/manual/en/reference.pcre.pattern.modifiers.php http://hu.php.net/manual/en/reference.pcre.pattern.syntax.php greets, Zoltán Németh Thanks, Jon -- jon bennett w: http://www.jben.net/ iChat (AIM): jbendotnet Skype: jon-bennett -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple RegEx to pull out content between 2 markers
Jon Bennett wrote: Hi, I need to grab the what's between 2 markers in a Textile / html string, but my regex skills aren't all that hot. The text I have is: OPEN_MAIN_COLUMN h3. Article Content Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. p(image). !/app/image/1/large! Caption p(image). !/app/image/24/large! drunken jonty CLOSE_MAIN_COLUMN OPEN_LEFT_COLUMN h3. Article Content !/app/image/7/thumb! Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. CLOSE_LEFT_COLUMN CLOSE_RIGHT_COLUMN h3. Article Content Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. * Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. * Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. * Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. CLOSE_RIGHT_COLUMN I'd like to grab the contents of left, right and main individually so I can place them in separate form fields. Any help greatly appreciated. I'm certain this is a cinch for people who use regex often. It's really not that hard if you use three separate expressions: $left = preg_match('/OPEN_LEFT_COLUMN(.*)CLOSE_LEFT_COLUMN/is', $text, $matches); $right = preg_match('/OPEN_RIGHT_COLUMN(.*)CLOSE_RIGHT_COLUMN/is', $text, $matches); $main = preg_match('/OPEN_MAIN_COLUMN(.*)CLOSE_MAIN_COLUMN/is', $text, $matches); And then, IIRC, the content you're after will be in $matches[1]. -- Richard Heyes Employ me: http://www.phpguru.org/cv -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple RegEx question
Find Weitz's The Regex Coach (Linux and Windows) and install it and play around. You'll learn more in one day from the pretty color syntax highlighting than reading the perl books/pages for months. On Mon, December 24, 2007 9:34 pm, M5 wrote: I'm learning regular expressions, and trying to figure out what's possible and what's not. Any ideas of how to create a preg_match expression to parse following three lines: Calgary, AB T2A6C1 Toronto, ON T4M 0B0 Saint John, NBE2L 4L1 ...such that it splits each line into City, Province and Postalcode (irrespective of occasional white space), e.g.: Array ( [city] = Calgary, [prov] = AB, [postal]= T2A 6C1 ) Array ( [city] = Toronto, [prov] = ON, [postal]= T4M 0B0 ) Array ( [city] = Saint John, [prov] = NB, [postal]= E2L 4L1 ) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Some people have a gift link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple RegEx question
M5 schreef: I'm learning regular expressions, and trying to figure out what's possible and what's not. pretty much anything as far as string parsing goes. Any ideas of how to create a preg_match expression to parse following three lines: yes. given your intention to learn regexps why not try to come up with one? if you get stuck show us what you have so far ... you don't learn to eat with cutlery by being spoonfed. if you want to parse the whole string in one go you'll want to use preg_match_all() otherwise use preg_match and iterate over each line .. foreach(explode(\n, $data) as $line) { $matches = array(); if (preg_match($line, $regexp, $matches)) { /* do something */ } } Calgary, AB T2A6C1 Toronto, ON T4M 0B0 Saint John, NBE2L 4L1 ...such that it splits each line into City, Province and Postalcode (irrespective of occasional white space), e.g.: Array ( [city]= Calgary, [prov]= AB, [postal]= T2A 6C1 ) Array ( [city]= Toronto, [prov]= ON, [postal]= T4M 0B0 ) Array ( [city]= Saint John, [prov]= NB, [postal]= E2L 4L1 ) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Simple RegEx question
I'm learning regular expressions, and trying to figure out what's possible and what's not. Any ideas of how to create a preg_match expression to parse following three lines: Calgary, AB T2A6C1 Toronto, ON T4M 0B0 Saint John, NBE2L 4L1 ...such that it splits each line into City, Province and Postalcode (irrespective of occasional white space), e.g.: Array ( [city] = Calgary, [prov] = AB, [postal]= T2A 6C1 ) Array ( [city] = Toronto, [prov] = ON, [postal]= T4M 0B0 ) Array ( [city] = Saint John, [prov] = NB, [postal]= E2L 4L1 ) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple RegEx question
On Dec 24, 2007, at 7:34 PM, M5 [EMAIL PROTECTED] wrote: I'm learning regular expressions, and trying to figure out what's possible and what's not. Any ideas of how to create a preg_match expression to parse following three lines: Calgary, AB T2A6C1 Toronto, ON T4M 0B0 Saint John, NBE2L 4L1 ...such that it splits each line into City, Province and Postalcode (irrespective of occasional white space), e.g.: Array ( [city]= Calgary, [prov]= AB, [postal]= T2A 6C1 ) Array ( [city]= Toronto, [prov]= ON, [postal]= T4M 0B0 ) Array ( [city]= Saint John, [prov]= NB, [postal]= E2L 4L1 ) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Try this: $places = array(); $lines = explode(\n, $toparse); foreach ($lines as $i = $line) list($places[$i]['city'], $places[$i]['prov'], $places[$i] ['postal']) = explode(' ', $line, 3);' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple RegEx question
On Dec 24, 2007, at 7:59 PM, Casey [EMAIL PROTECTED] wrote: On Dec 24, 2007, at 7:34 PM, M5 [EMAIL PROTECTED] wrote: I'm learning regular expressions, and trying to figure out what's possible and what's not. Any ideas of how to create a preg_match expression to parse following three lines: Calgary, AB T2A6C1 Toronto, ON T4M 0B0 Saint John, NBE2L 4L1 ...such that it splits each line into City, Province and Postalcode (irrespective of occasional white space), e.g.: Array ( [city]= Calgary, [prov]= AB, [postal]= T2A 6C1 ) Array ( [city]= Toronto, [prov]= ON, [postal]= T4M 0B0 ) Array ( [city]= Saint John, [prov]= NB, [postal]= E2L 4L1 ) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Try this: $places = array(); $lines = explode(\n, $toparse); foreach ($lines as $i = $line) list($places[$i]['city'], $places[$i]['prov'], $places[$i] ['postal']) = explode(' ', $line, 3);' I'm very sorry about that, Ive been wrong all week! It doesn't parse Saint John correctly :( -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] simple regex query
Hi guys Been tryin to figure out regex and have found some tutorials but some have made things clear and others have confused me. Anyway for a simple query, if I just wanted to check that a variable has only text and numeric characters would I do something like this (i want it to fail if it finds a symbol eg: + - { etc...): echo REG result: . preg_match('/[a-zA-Z0-9]*/', '{d-fg'); however this resolves to true because there are normal characters (alphabetical characters) so how do I make the expression fail because of the { and - characters in the string? You can also list which characters you DON'T want -- just use a '^' as the first symbol in a bracket expression (i.e., %[^a-zA-Z]% matches a string with a character that is not a letter between two percent signs). But that would mean that I would have to list each symbol I dont want and that would be undesireable as it would be better to list exactly what the acceptable characters are. Can anyone give me some insight as to where I'm going wrong? thanks -- Angelo -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] simple regex query
Angelo Zanetti wrote: Hi guys Been tryin to figure out regex and have found some tutorials but some have made things clear and others have confused me. Anyway for a simple query, if I just wanted to check that a variable has only text and numeric characters would I do something like this (i want it to fail if it finds a symbol eg: + - { etc...): echo REG result: . preg_match('/[a-zA-Z0-9]*/', '{d-fg'); you're on the right path, whats needed is start and end [string] delimiters in the regexp (note I used a different regexp delimiter, '#', which is irrelevant): echo REG 1 result: , preg_match(#[a-zA-Z0-9]*#, {d-fg), \n, REG 2 result: , preg_match(#^[a-zA-Z0-9]*$#, {d-fg), \n; the magic characters are '^' andf '$' as explained in more detail here: http://php.net/manual/en/reference.pcre.pattern.syntax.php quote ^ assert start of subject (or line, in multiline mode) $ assert end of subject (or line, in multiline mode) /quote however this resolves to true because there are normal characters (alphabetical characters) so how do I make the expression fail because of the { and - characters in the string? You can also list which characters you DON'T want -- just use a '^' as the first symbol in a bracket expression (i.e., %[^a-zA-Z]% matches a string with a character that is not a letter between two percent signs). But that would mean that I would have to list each symbol I dont want and that would be undesireable as it would be better to list exactly what the acceptable characters are. Can anyone give me some insight as to where I'm going wrong? thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] simple regex query
Jochem Maas wrote: Angelo Zanetti wrote: Hi guys Been tryin to figure out regex and have found some tutorials but some have made things clear and others have confused me. Anyway for a simple query, if I just wanted to check that a variable has only text and numeric characters would I do something like this (i want it to fail if it finds a symbol eg: + - { etc...): echo REG result: . preg_match('/[a-zA-Z0-9]*/', '{d-fg'); you're on the right path, whats needed is start and end [string] delimiters in the regexp (note I used a different regexp delimiter, '#', which is irrelevant): echo REG 1 result: , preg_match(#[a-zA-Z0-9]*#, {d-fg), \n, REG 2 result: , preg_match(#^[a-zA-Z0-9]*$#, {d-fg), \n; the magic characters are '^' andf '$' as explained in more detail here: http://php.net/manual/en/reference.pcre.pattern.syntax.php quote ^ assert start of subject (or line, in multiline mode) $ assert end of subject (or line, in multiline mode) /quote thanks but I think I kinda got working the other way around: if (!preg_match('/[^a-zA-Z0-9]/', 'gf-5')) echo valid; else echo invalid; then if I wanted to list any symbols I could just change it to: preg_match('/[^a-zA-Z0-9\,\.]/', 'gf-5')) if I wanted . and , to be accepted. tx -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] simple regex query
On 06/04/06, Angelo Zanetti [EMAIL PROTECTED] wrote: Anyway for a simple query, if I just wanted to check that a variable has only text and numeric characters would I do something like this (i want it to fail if it finds a symbol eg: + - { etc...): echo REG result: . preg_match('/[a-zA-Z0-9]*/', '{d-fg'); however this resolves to true because there are normal characters (alphabetical characters) so how do I make the expression fail because of the { and - characters in the string? You can also list which characters you DON'T want -- just use a '^' as the first symbol in a bracket expression (i.e., %[^a-zA-Z]% matches a string with a character that is not a letter between two percent signs). But that would mean that I would have to list each symbol I dont want and that would be undesireable as it would be better to list exactly what the acceptable characters are. You're pretty much there. Your first example resolved to true because you didn't anchor the start and end of the expression. /^[a-z0-9]*$/i It would be a bit more efficient to use the negated character class you mentioned. /[^a-z0-9]/i All you need to bear in mind is that preg_match() would then return true if the string contains an illegal character and false if it is OK rather than the other way around. So these should both produce the same result: preg_match('/^[a-z0-9]*$/i', '{d-fg'); !preg_match('/[^a-z0-9]/i', '{d-fg'); -robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] simple regex query
On Thursday 06 April 2006 6:19 am, Angelo Zanetti wrote: Hi guys Been tryin to figure out regex and have found some tutorials but some have made things clear and others have confused me. Anyway for a simple query, if I just wanted to check that a variable has only text and numeric characters would I do something like this (i want it to fail if it finds a symbol eg: + - { etc...): echo REG result: . preg_match('/[a-zA-Z0-9]*/', '{d-fg'); however this resolves to true because there are normal characters (alphabetical characters) so how do I make the expression fail because of the { and - characters in the string? You can also list which characters you DON'T want -- just use a '^' as the first symbol in a bracket expression (i.e., %[^a-zA-Z]% matches a string with a character that is not a letter between two percent signs). But that would mean that I would have to list each symbol I dont want and that would be undesireable as it would be better to list exactly what the acceptable characters are. Can anyone give me some insight as to where I'm going wrong? thanks -- Angelo I found an AJAX regex tester the other day. It'll check PCRE, Posix, and Javascript. Don't know how useful this is, but thought I'd throw it into this thread. http://rexv.org/ -- Joe Henry www.celebrityaccess.com [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] simple regex query
On Thu, 2006-04-06 at 15:36 +0200, Jochem Maas wrote: Been tryin to figure out regex and have found some tutorials but some have made things clear and others have confused me. We are busy building up a library of commonly used regex's on a wiki, check it out at http://fsiu.uwc.ac.za/ If its not there, please add a method once you figure it out! --Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Simple REGEX?
well, if the only characters that you want to allow are alphanum/space/underscore, you could use a perl regexp to match against anything *other* than those things...(a positive match would indicate that the string being matched had invalid characters in it) use this: preg_match('/[^\w\s]/', $input); (to see an explanation of why this works, please read the bottom of my email... i give a short breakdown of it) if you want to allow any more characters, simply place the character you want to allow within the brackets, and any instance of that character in the string to be matched will be ignored (rather than matched) for example, to allow periods and question marks in your input (without getting a positive match on the regexp), modify it to look like this: '/[^\w\s\.\?]/' what will match and what will not match using this regexp: print preg_match('/[^\w\s]/', $t); $t = "ea!ti_t99"; will print "1" since the "!" will match the regexp $t = "Hello one_two" will print "0" since nothing matches $t = "How *are* you" will print "1" since the "*" matches the regexp works like this: [ ] = character class ^ = not \w = alphanumeric \s = space the character class brackets are just to let the regexp know that the things inside of it are to be matched one by one against the string. "[suv]" would not match the entire string "suv", but rather "s" or "u" or "v", so "[\w\s]" would match any string that had an alphanumeric character or whitespace in it. since you want to find strings that have NOT-alphanum or NOT-space, you simply negate the character class that will match alphanum and space, by adding a "^" to it... making it "[^\w\s]" (match anything that is NOT an alphanumeric or space character - meaning match any string that has invalid characters). if you want to allow other characters into your input, just add them into the character class. if you find that you need to allow "?" into your input, use this "[^\w\s\?]"... and it will match any string that does NOT have alphanum/space/?, meaning that it will match any string with invalid chars. -Original Message- From: James, Yz [mailto:[EMAIL PROTECTED]] Sent: Saturday, February 17, 2001 12:15 To: [EMAIL PROTECTED] Subject: [PHP] Simple REGEX? Hi guys, I'd like to restrict the characters a client is inputting to a database. I already have a system that'll exclude certain characters. Alpha Numerics are permitted, together with the underscore and spaces. Is there a collective expression for the following characters? : !",.?@() Because I'd like to allow those too. And I'm useless with RegEx. Thanks as always, James. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Simple REGEX?
Hi guys, I'd like to restrict the characters a client is inputting to a database. I already have a system that'll exclude certain characters. Alpha Numerics are permitted, together with the underscore and spaces. Is there a collective expression for the following characters? : !",.?@() Because I'd like to allow those too. And I'm useless with RegEx. Thanks as always, James. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]