New topic: 

How do you split a string by using a regular expression ?

<http://forums.realsoftware.com/viewtopic.php?t=45237>

         Page 1 of 1
   [ 2 posts ]                 Previous topic | Next topic          Author  
Message        Cesco          Post subject: How do you split a string by using 
a regular expression ?Posted: Mon Sep 03, 2012 3:20 pm                         
Joined: Fri Mar 09, 2007 2:04 am
Posts: 90
Location: Venice, Italy                Is it possible to split a string by 
using a regular expression? I had a very shallow look at the manual so maybe I 
have not seen the right command to do this operation.
For example I need to split a string like this that has some random Barry White 
songs listed:

Let the music play, You're my first\, my last\, my everything, Just the way you 
are

I need to split the string when a comma is found *BUT* only if the comma is not 
escaped (preceded by a backslash). So in this example I'm expecting to get an 
array with three elements:

0: Let the music play
1: You're my first\, my last\, my everything
2: Just the way you are


In PHP I'd probably write something like this: $arrayOfSongs = preg_split( 
"/(?<!\\\\)\\,/s", $string )
Do you know if there is any method in the RegEx class to do this kind of split 
directly or do I have to write my own function to get the same results ?


Thank you   
                             Top                npalardy          Post subject: 
Re: How do you split a string by using a regular expressionPosted: Mon Sep 03, 
2012 4:29 pm                       Real Software Engineer          
Joined: Sat Dec 24, 2005 8:18 pm
Posts: 7402
Location: Canada, Alberta, Near Red Deer                in Real Studio you can 
use a reg ex to match each portion
Then you just do the searches in a loop & it will grab successive portions

dim r as regex
// ... set up the regex with the right pattern
dim rm as regexmatch = r.search( stringWithDataToSplit )
while rm <> nil
  // do something with the match
  // like append it to an array
  
  // and repeat the search to find the next match
  rm = r.search( )
wend


of course IF this is the only criteria you might just use a replacall with a 
split

dim intermediate as string = replaceall( stringWithDataToSplit, "\," , chrb(1) )
dim strings(-1) as string = split( intermediate, "," )
for i as integer = 0 to ubound(strings)
  strings(i) = replaceall( strings(i), chrb(1),  "\," )
next
      
_________________
My web site Great White Software
RBLibrary.com REALbasic learning  
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 2 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

rbforumnotifier@monkeybreadsoftware.de

Reply via email to