The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 23, 2019, 06:50:09 AM

Login with username, password and session length


Pages: [1] 2
  Print  
Author Topic: Reg ex, explode how should i think about this correctly?  (Read 5216 times)
jammaster82
Lifer
*****
Offline Offline

Posts: 666


Thats craigs list for ya


View Profile
« on: November 22, 2009, 07:40:55 PM »

 Need Help

Ok i would like to 'explode' based on a regex pattern ..

I have a string:

$mystr = 'the data before the number 223. The data after the number. This data is after the number';

I want to explode on the number '223' and have the data before as an element and the data after as a separate element. I could:

 $myarray = explode('223',$mystr);

 but it could be *ANY* number, followed by a period and then a #32..

Is there some way to Collect up until [any number of numbers][a period][a #32] into a variable?
Like exploding on a pattern?


Logged

The watched pot, never boils... But if you walk away from it , the soup burns.  What gives?
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #1 on: November 23, 2009, 06:46:18 AM »

First of all, you could easily do this with preg_match_all, but you probably want to use preg_split.

If you use preg_match_all, just use groupings. if you did something like:

preg_match_all('/(\D+)\d+(\D)/', $buff, $matches);

then $matches[1][1] would contain all of the first set of parentheses, and $matches[1][2] would contain the matches from the second set of parens.
Logged

hai
jammaster82
Lifer
*****
Offline Offline

Posts: 666


Thats craigs list for ya


View Profile
« Reply #2 on: November 24, 2009, 01:32:45 AM »

/(\D+)\d+(\D)/

Thanks! hurts my brain still to look at ...

im trying to find some sort of chart with all the codes... any suggestions?


Logged

The watched pot, never boils... But if you walk away from it , the soup burns.  What gives?
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: November 24, 2009, 09:40:53 AM »

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

And buy the book. Superb.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
jammaster82
Lifer
*****
Offline Offline

Posts: 666


Thats craigs list for ya


View Profile
« Reply #4 on: November 24, 2009, 12:20:06 PM »

awesome... theres a lot of complexity to this.. i like the
'bug' walking through the file collecting backwards and forwards
pattern match controlled style better less wear on my gears Wink

after all the reading i did and i finally came up with this:
Looks like the robot help screen to me. 




* regular_expression_reference_chart.PNG (67.32 KB, 1155x996 - viewed 266 times.)
Logged

The watched pot, never boils... But if you walk away from it , the soup burns.  What gives?
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #5 on: November 24, 2009, 12:38:35 PM »

awesome... theres a lot of complexity to this.. i like the
'bug' walking through the file collecting backwards and forwards
pattern match controlled style better less wear on my gears Wink
Regex are awesome. An incredible tool to have in the bucket.


Looks like the robot help screen to me. 
Wink do you still have all the code and a running instance of TRobot?
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #6 on: November 24, 2009, 02:34:23 PM »

a decent regex site: http://4sp.in/2oh
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #7 on: November 24, 2009, 02:55:39 PM »

LOL nuts is so in love with 4sp.in that he didn't notice it's the same as my link above...
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
jammaster82
Lifer
*****
Offline Offline

Posts: 666


Thats craigs list for ya


View Profile
« Reply #8 on: November 25, 2009, 12:51:03 AM »

pretty neat url shrinker... great to hide links..

@robot.. theres a chance the executable is in a cd
in a jewel case in mamas attic... i sent you the
delphi stuff i had... the ability to parse BACKWARDS
made things easier to chew up IMO


 
Logged

The watched pot, never boils... But if you walk away from it , the soup burns.  What gives?
jammaster82
Lifer
*****
Offline Offline

Posts: 666


Thats craigs list for ya


View Profile
« Reply #9 on: November 29, 2009, 11:39:34 AM »

Im getting this error:

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash

but if i use preg_split

*it works..*

___ if i test it here:

http://www.switchplane.com/utilities/preg_match-regular-expression-tester.php?pattern=%2F\n[a-z|A-z].%2F&subject=the+first+line%0D%0AA.+the+line+i+want+to+match+the+beginning+of%0D%0Asomeother+line+that+doesnt+start+that+way%0D%0A

     * it WORKS *


it returns the match i want..

What gives?

Code:
$buff = <<<HTML
the first line
A. the line i want to match the beginning of
someother line that doesnt start that way
HTML;

$re="/\n[a-z|A-z].]/";

$matches = preg_match($buff,$re);

echo "<textarea rows=20 cols=80>";
//echo "";
print_r($matches);
echo "</textarea></br>";
?>
Logged

The watched pot, never boils... But if you walk away from it , the soup burns.  What gives?
jammaster82
Lifer
*****
Offline Offline

Posts: 666


Thats craigs list for ya


View Profile
« Reply #10 on: November 29, 2009, 07:54:20 PM »

the answer is, im retarded

preg_match is preg_match(pattern, subject) i had it subject, pattern

:rolls eyes: god, im a dork
Logged

The watched pot, never boils... But if you walk away from it , the soup burns.  What gives?
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #11 on: November 29, 2009, 08:19:10 PM »

Can't tell you how many times I've done that. Glad you've got it sorted JM
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
dirk
Global Moderator
Expert
*****
Offline Offline

Posts: 416


View Profile
« Reply #12 on: November 29, 2009, 09:24:53 PM »

Here is another good regex guide:

http://net.tutsplus.com/tutorials/javascript-ajax/you-dont-know-anything-about-regular-expressions/
Logged
jammaster82
Lifer
*****
Offline Offline

Posts: 666


Thats craigs list for ya


View Profile
« Reply #13 on: November 30, 2009, 10:20:05 AM »

Can't tell you how many times I've done that. Glad you've got it sorted JM

come on, its ME you can tell ME@~  hahaha ya that's what happens when you try to program and get screamed at at the same time. hahaha

@perkiset... so how do you approach text file parsing, with regexp? do you ever walk through strings, logo style , what is the best way to think about parsing text with php? i guess im having trouble switching from one pass through mentality to regex have you posted any code here at the cache that parses text i can take a lookie lou at?

@dirk: thanks that site is excellent vidtuts and all...  Praise
Logged

The watched pot, never boils... But if you walk away from it , the soup burns.  What gives?
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #14 on: November 30, 2009, 10:26:23 AM »

@perkiset... so how do you approach text file parsing, with regexp? do you ever walk through strings, logo style , what is the best way to think about parsing text with php? i guess im having trouble switching from one pass through mentality to regex have you posted any code here at the cache that parses text i can take a lookie lou at?

Very much on a case-by-case basis.

The serial walk-through that we used to be so good at is often inappropriate today, given that the data you're looking at doesn't look like an airline ticket Wink although that said, many of the regex functions we have available would've been a BIG help back then.

I rarely do complete walkthroughs anymore, preferring locating the things I want via either regex or other PHP functions that clean/extract things for me. It's most often easier and quicker to extract than it is to "traditionally parse" - except when data is co-mingled with surrounding data in such a way that simple extract is virtually impossible, whereby I'll more like trim and "scootch in" on the data than try to write a wicked complex regex to get at it.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1] 2
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!