Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regular expression

Reply
Thread Tools

Regular expression

 
 
sk
Guest
Posts: n/a
 
      04-10-2007
I m trying to add a extra keyword on a specific word in a html document
In the following document I want to put "My" in front of all the
words"computer"
excluding the id for the stylesheet and name of image files.

<html>
<head>
<title>hello</titile>
</head>
<body>
<div id="computer"> This is computer</div>
<img src="images/computer.jpg" >Computer
</body>
</html>



 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      04-10-2007
sk wrote:
> I m trying to add a extra keyword on a specific word in a html document
> In the following document I want to put "My" in front of all the
> words"computer"
> excluding the id for the stylesheet and name of image files.
>
> <html>
> <head>
> <title>hello</titile>
> </head>
> <body>
> <div id="computer"> This is computer</div>
> <img src="images/computer.jpg" >Computer
> </body>
> </html>


s/\scomputer/ My computer/;
 
Reply With Quote
 
 
 
 
4i4ko Trevi4ko
Guest
Posts: n/a
 
      04-10-2007
On Tue, 10 Apr 2007 15:41:25 -0500, J. Gleixner wrote:

> sk wrote:
>> I m trying to add a extra keyword on a specific word in a html document
>> In the following document I want to put "My" in front of all the
>> words"computer"
>> excluding the id for the stylesheet and name of image files.
>>
>> <html>
>> <head>
>> <title>hello</titile>
>> </head>
>> <body>
>> <div id="computer"> This is computer</div>
>> <img src="images/computer.jpg" >Computer
>> </body>
>> </html>

>
> s/\scomputer/ My computer/;


will not work on:
<img src="images/computer.jpg" >Computer

 
Reply With Quote
 
guillaume.carbonneau@gmail.com
Guest
Posts: n/a
 
      04-10-2007
On Apr 10, 4:43 pm, 4i4ko Trevi4ko <(E-Mail Removed)> wrote:
> On Tue, 10 Apr 2007 15:41:25 -0500, J. Gleixner wrote:
> > sk wrote:
> >> I m trying to add a extra keyword on a specific word in a html document
> >> In the following document I want to put "My" in front of all the
> >> words"computer"
> >> excluding the id for the stylesheet and name of image files.

>
> >> <html>
> >> <head>
> >> <title>hello</titile>
> >> </head>
> >> <body>
> >> <div id="computer"> This is computer</div>
> >> <img src="images/computer.jpg" >Computer
> >> </body>
> >> </html>

>
> > s/\scomputer/ My computer/;

>
> will not work on:
> <img src="images/computer.jpg" >Computer


I got it through 2 regular expressions. Maybe someone can help me
merge them together...

$string =~ s/(>.*)(computer)(.*<)/\1my \2\3/igms;
$string =~ s/(>.*)(computer)(.*<)/\1my \2\3/ig;

 
Reply With Quote
 
sk
Guest
Posts: n/a
 
      04-10-2007

"J. Gleixner" <(E-Mail Removed)> wrote in message
news:461bf675$0$496$(E-Mail Removed)...
> sk wrote:
>> I m trying to add a extra keyword on a specific word in a html document
>> In the following document I want to put "My" in front of all the
>> words"computer"
>> excluding the id for the stylesheet and name of image files.
>>
>> <html>
>> <head>
>> <title>hello</titile>
>> </head>
>> <body>
>> <div id="computer"> This is computer</div>
>> <img src="images/computer.jpg" >Computer
>> </body>
>> </html>

>
> s/\scomputer/ My computer/;


what is \s? a whole word? Is there another way to do this?
Sometimes i define the function name like "GetComputer".
I want to be able to change to "GetMyComputer"


 
Reply With Quote
 
guillaume.carbonneau@gmail.com
Guest
Posts: n/a
 
      04-10-2007
On Apr 10, 5:22 pm, "sk" <(E-Mail Removed)> wrote:
> "J. Gleixner" <(E-Mail Removed)> wrote in message
>
> news:461bf675$0$496$(E-Mail Removed)...
>
>
>
> > sk wrote:
> >> I m trying to add a extra keyword on a specific word in a html document
> >> In the following document I want to put "My" in front of all the
> >> words"computer"
> >> excluding the id for the stylesheet and name of image files.

>
> >> <html>
> >> <head>
> >> <title>hello</titile>
> >> </head>
> >> <body>
> >> <div id="computer"> This is computer</div>
> >> <img src="images/computer.jpg" >Computer
> >> </body>
> >> </html>

>
> > s/\scomputer/ My computer/;

>
> what is \s? a whole word? Is there another way to do this?
> Sometimes i define the function name like "GetComputer".
> I want to be able to change to "GetMyComputer"


\s is whitespaces

 
Reply With Quote
 
Tony Curtis
Guest
Posts: n/a
 
      04-11-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Apr 10, 4:43 pm, 4i4ko Trevi4ko <(E-Mail Removed)> wrote:
>> On Tue, 10 Apr 2007 15:41:25 -0500, J. Gleixner wrote:
>>> sk wrote:
>>>> I m trying to add a extra keyword on a specific word in a html document
>>>> In the following document I want to put "My" in front of all the
>>>> words"computer"
>>>> excluding the id for the stylesheet and name of image files.
>>>> <html>
>>>> <head>
>>>> <title>hello</titile>
>>>> </head>
>>>> <body>
>>>> <div id="computer"> This is computer</div>
>>>> <img src="images/computer.jpg" >Computer
>>>> </body>
>>>> </html>
>>> s/\scomputer/ My computer/;

>> will not work on:
>> <img src="images/computer.jpg" >Computer

>
> I got it through 2 regular expressions. Maybe someone can help me
> merge them together...
>
> $string =~ s/(>.*)(computer)(.*<)/\1my \2\3/igms;
> $string =~ s/(>.*)(computer)(.*<)/\1my \2\3/ig;


A regex is the wrong (general) solution here. You need an HTML parser.

hth
t
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      04-11-2007
sk wrote:
> I m trying to add a extra keyword on a specific word in a html
> document In the following document I want to put "My" in front of all
> the words"computer"
> excluding the id for the stylesheet and name of image files.


As has been mentioned a gazillion times in this NG parsing HTML correctly is
not easy and only a masochist would try to do it using REs. It is very easy
to find sample HTML code, that breaks any RE construct that you can come up
with. See the FAQ "How do I remove HTML from a string?" for some examples
that you probably forgot to consider.

Instead just use a proper HTML parser from CPAN. They come ready-made and
most important they actualy work.

jue


 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      04-15-2007
On 2007-04-10 21:17, (E-Mail Removed) <(E-Mail Removed)> wrote:
> On Apr 10, 4:43 pm, 4i4ko Trevi4ko <(E-Mail Removed)> wrote:
>> On Tue, 10 Apr 2007 15:41:25 -0500, J. Gleixner wrote:
>> > sk wrote:
>> >> I m trying to add a extra keyword on a specific word in a html
>> >> document In the following document I want to put "My" in front of
>> >> all the words"computer" excluding the id for the stylesheet and
>> >> name of image files.

>>
>> >> <html>
>> >> <head>
>> >> <title>hello</titile>
>> >> </head>
>> >> <body>
>> >> <div id="computer"> This is computer</div>
>> >> <img src="images/computer.jpg" >Computer
>> >> </body>
>> >> </html>

>>
>> > s/\scomputer/ My computer/;

>>
>> will not work on:
>> <img src="images/computer.jpg" >Computer

>
> I got it through 2 regular expressions. Maybe someone can help me
> merge them together...
>
> $string =~ s/(>.*)(computer)(.*<)/\1my \2\3/igms;


That will match
$1: From the first ">" to the ">" in the img tag in line 7.
$2: "Computer" in line 7
$3: From the newline after "Computer" to the opening "<" in
"</html".

In other words it will only replace the last "Computer" in the file.
The "g" is ineffective, since there can be only one last "computer".
The "m" doesn't seem to have any effect either.

> $string =~ s/(>.*)(computer)(.*<)/\1my \2\3/ig;


This will replace any "computer" after a ">" and before a "<" on the
same line. In the example file there is only one "computer" left which
matches that criterion, but that doesn't seem guaranteed.

hp

--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | (E-Mail Removed) |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-15-2007
Peter J. Holzer <(E-Mail Removed)> wrote:
> On 2007-04-10 21:17, (E-Mail Removed) <(E-Mail Removed)> wrote:



>> $string =~ s/(>.*)(computer)(.*<)/\1my \2\3/igms;



> The "m" doesn't seem to have any effect either.



"m" only affects the ^ and $ anchors.

So it is a no-op for patterns that do not contain those anchors.

(but no sensible person would adopt code that generates easy-to-avoid
warnings anyway.)


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments