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: Regular Expressions:
multiple repeating patterns

 



XeJingle
New User

Nov 28, 2007, 11:05 AM

Post #1 of 6 (6029 views)
multiple repeating patterns Can't Post

Hi,

I am working a lot with regexp, but this is one problem, which could maybe be solved with a regexp oneliner but I am not able to get it ... maybe it is not even possible with one line.

Text looks like this:


Code
uierhuierg heguirghrthg rth grtguirtui 
uirt ui ghrtguhrtg rui rgrtgrt uigrrthgrtgrtug
* something
* something else
* something else 2
* ...
er uihgiurthg rgtui trhg g uhrthg rtruuirtgrt
g iuorthguirthuirtghrt uhrtgrtu



* (asterisk) describes a list. text can contain undefined number of these lines one after each other. I want to replace * to make it look like this:


Code
uierhuierg heguirghrthg rth grtguirtui 
uirt ui ghrtguhrtg rui rgrtgrt uigrrthgrtgrtug
<ul>
<li>something</li>
<li>something else</li>
<li>something else 2</li>
<li>...</li>
</ul>
er uihgiurthg rgtui trhg g uhrthg rtruuirtgrt
g iuorthguirthuirtghrt uhrtgrtu



it is no a problem to match the asterisks and text, but how do replace it effectively.

I played with some versions of the following:
$text =~s/(((\*\s*)(.*?)(\r?\n)*)+)/\<ul\>\<li\>$4\<\/li\>\<\/ul\>/g;


that would put every line beginning with * in <ul><li>something</li></ul>.

Is it somehow possible to do this with one line regexp? Or should I try to make longer but easier and match everything separately.

Anyone with an idea?

Thanks.


winfinit
User

Dec 5, 2007, 10:42 PM

Post #2 of 6 (6014 views)
Re: [XeJingle] multiple repeating patterns [In reply to] Can't Post

well, if i understand correctly, then this should do it

#!/usr/bin/perl
$OriginalFile = 'ChangeThisToYourFileName';

$ProcessedFile= $OriginalFile .'.PROCESSED';
open (RAWFILE, "$OriginalFile");
open (NEWFILE, "> $ProcessedFile");

while (<RAWFILE>)
{
if(/^[\s]?\*/)
{
$_ =~ s|^[\s]?\*(.*)|<li>$1</li>|;
}
print NEWFILE;
}


michaelaz
New User

Dec 6, 2007, 2:24 AM

Post #3 of 6 (6012 views)
Re: [winfinit] multiple repeating patterns [In reply to] Can't Post

i am not readinf from a file but getting the output from the buffer and the output include the char > which is the prompt sign

the qeustion is to set a regular experssion asking it to look for the char > as a prompt (first char in the line ??)

and not the char > that come in the middle of the line


winfinit
User

Dec 6, 2007, 7:56 AM

Post #4 of 6 (6011 views)
Re: [michaelaz] multiple repeating patterns [In reply to] Can't Post

ok, then answer is above anyways, to match something at the beginning of a line you just have to use ^ in front of your regex so if you want to match > at the beginning of a line then your regex would be /^>/


XeJingle
New User

Dec 7, 2007, 9:49 AM

Post #5 of 6 (5994 views)
Re: [winfinit] multiple repeating patterns [In reply to] Can't Post


In Reply To
well, if i understand correctly, then this should do it

#!/usr/bin/perl
$OriginalFile = 'ChangeThisToYourFileName';

$ProcessedFile= $OriginalFile .'.PROCESSED';
open (RAWFILE, "$OriginalFile");
open (NEWFILE, "> $ProcessedFile");

while (<RAWFILE>)
{
if(/^[\s]?\*/)
{
$_ =~ s|^[\s]?\*(.*)|<li>$1</li>|;
}
print NEWFILE;
}


No, that is not that. There is no "<ul>" & "</ul>" in your code.

RegExp should replace one or more lines beginning with "^\*\s+" with <ul><li>....</li></ul>.

Example :

* text 1
* text 2

onetowthree

* text 3
* text 4
* text 5

should be:
<ul>
<li>text 1</li>
<li>text 2</li>
</ul>

onetowthree

<ul>
<li>text 3</li>
<li>text 4</li>
<li>text 5</li>
</ul>

Thanks.


KevinR
Veteran


Dec 7, 2007, 12:35 PM

Post #6 of 6 (5991 views)
Re: [XeJingle] multiple repeating patterns [In reply to] Can't Post

It could very well be possible to do what you want using one regexp, I am not going to attempt to write that solution. Here is one that should work:


Code
use strict; 
use warnings;

my $file = 'path/to/file.txt';

{
local @ARGV = ($file);
local $^I = '.bak';
MAIN: while ( <> ) {
chomp;
if (s/^\*\s*//) {
print "<ul>\n<li>$_</li>\n";
while ( <> ) {
chomp;
if (!/^\*/) {
print "</ul>\n$_\n";
next MAIN;
}
else {
s/^\*\s*//;
print "<li>$_</li>\n";
}
}
}
else {
print "$_\n";
}
}
}
print 'finished';

-------------------------------------------------

 
 


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

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