CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Tokenizing

 



BeatF6
New User

Jul 13, 2013, 2:46 PM

Post #1 of 4 (470 views)
Tokenizing Can't Post

Hi @ all

With my script (which is big), I want to find the following sequence:

parenthesis + SOMETHING + parenthesis + comma

["(" + ... + ")" + ","]

If this is found, I'd like to insert a space after the second parenthesis such that this is splitted afterwards in my script. How can that be done? Thanks for any help and propositions.

I have an example, where all the names needed are present:


Code
if($line =~ m/^([^. \t]+\w\.)([A-Z][^ \240]*)/) # treats words that should be split 
{
$line =~ s/(?<=[^. \t]\w\.)(?=[A-Z])/ /g;
}



Laurent_R
Veteran / Moderator

Jul 14, 2013, 1:59 AM

Post #2 of 4 (467 views)
Re: [BeatF6] Tokenizing [In reply to] Can't Post

To really give a reliable answer we would need to know what the SOMETHING is.

This is quick test under the Perl debugger:


Code
  DB<1> $string = "the quick brown (fox), the (lazy), dog."; 

DB<2> $string =~ s/(\(.+?\)),/$1 ,/g;

DB<3> p $string
the quick brown (fox) , the (lazy) , dog.
DB<4>


So it seems to be doing what you want. But it might not work if you have nested parens with commas in your SOMETHING.


BeatF6
New User

Jul 14, 2013, 2:07 AM

Post #3 of 4 (464 views)
Re: [Laurent_R] Tokenizing [In reply to] Can't Post

Ok, so the correct implementation would be:


Code
 
if($line =~ m/^([^. \t]+\w\.)([A-Z][^ \240]*)/) # treats words that should be split
{
$line =~ s/(\(.+?\)),/$1 ,/g;
}


(I'm a newbie, so...sorry for this stupid question, but I'd like to be sure ;) )


Laurent_R
Veteran / Moderator

Jul 14, 2013, 2:13 AM

Post #4 of 4 (463 views)
Re: [BeatF6] Tokenizing [In reply to] Can't Post

Assuming your m// regex is correct (which I cannot confirm, not knowing what it is supposed to do), yes.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives