How to find the end of a word in perl..

S

Sooraj S

Hi,

My data file :
-------------------
// abc def
//
// abc dser
//
----------------------
i want to remove the lines 2 and 4 from the file.

check criteria : line should start with // and one or more spaces only
as the other characters.

i tried this .. if (/^\/\/(\s)*/ ... but it shows entire lines..

How to do it using regular expressions ? Is there any way to find the
end of a word using regular expressions ?
 
J

Jürgen Exner

Sooraj S said:
Hi,

My data file :
-------------------
// abc def
//
// abc dser
//
----------------------
i want to remove the lines 2 and 4 from the file.

check criteria : line should start
^

with //
//

and one or more spaces only

+ (that is a space character followed by a plus sign)
as the other characters.

$

So the whole RE becomes
(^// +$)
i tried this .. if (/^\/\/(\s)*/ ... but it shows entire lines..

How to do it using regular expressions ?

You are plenking.
Is there any way to find the
end of a word using regular expressions ?

Sure, from 'perldoc perlre':

Assertions
Perl defines the following zero-width assertions:
\b Match a word boundary

But what does that question have to do with your earlier problem
statement?

jue
 
J

Justin C

Hi,

My data file :
-------------------
// abc def
//
// abc dser
//
----------------------
i want to remove the lines 2 and 4 from the file.

check criteria : line should start with // and one or more spaces only
as the other characters.

i tried this .. if (/^\/\/(\s)*/ ... but it shows entire lines..

How to do it using regular expressions ? Is there any way to find the
end of a word using regular expressions ?

I think you want a '+' where you have '*'. '*' means 'any number of
times including 0'. '+' means 'at least once'.

However, that will still match the lines that you want to keep. You have
the '^' to match at the beginning of the string. If only there was
something to match the end of the string...

Justin.

PS Regex word boundary = \b
 
C

C.DeRykus

Hi,

My data file :
-------------------
// abc def
//
//    abc dser
//
----------------------
i want to remove the lines 2 and 4 from the file.

check criteria : line should start with // and one or more spaces only
as the other characters.

i tried this ..  if (/^\/\/(\s)*/ ... but it shows entire lines..

How to do it using regular expressions ? Is there any way to find the
end of a word using regular expressions ?


If non-whitespace is the secondary criterion for validity:


print if m{^ // \s+ (?=.*?\S) }x; # plenken/klempen frei
 
C

C.DeRykus

If non-whitespace is the secondary criterion for validity:

print if m{^ // \s+ (?=.*?\S) }x;  # plenken/klempen frei

or lookahead free too:

print if m{^ // \s+ \S }x;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top