I have a text file with lines like this
� Experience in C/C++ realtime system programming
� Experience in ACE, FIX
Could anybody tell me which regex to use to get rid of the dot and the
leading spaces before each Line?
Thanks for any help!
Jim
JG> I have a text file with lines like this
JG> · Experience in C/C++ realtime system programming
JG> · Experience in ACE, FIX
JG> Could anybody tell me which regex to use to get rid of the dot and the
JG> leading spaces before each Line?
what have you tried? do you have any code at all to show? this list
isn't for coding for you but to help perl beginners. this is a fairly
easy problem so why don't you do a basic s/// op on a line, anchor it to
the beginning and try to match what you don't want and replace it with
a null string. i wrote it in english so you have to translate that to
perl.
uri
--
Uri Guttman ------ u...@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
s/^\.\s*//;
I am reading "learning perl" but have not proceeded to regex chapters.
I googled and got to know hot to delete preceding white space. but now
the problem is that there is an odd dot at the beginning...
I don't know how to write the pattern in s/pattern//, so a regex for
that will do. Sorry if it sounds stupid, anyway, Thanks for the help!
Jim
Thanks Jim, I will figure out as I read "learning perl".
>
>
> --
> To unsubscribe, e-mail: beginners-...@perl.org
> For additional commands, e-mail: beginne...@perl.org
> http://learn.perl.org/
>
>
>
$_ =~ s/^\.(\s)+//g;
Cheers,
Parag
On Tue, Dec 22, 2009 at 10:59 AM, Jim Green <zhang.z...@gmail.com>wrote:
> Hi,
>
> I have a text file with lines like this
>
>
> · Experience in C/C++ realtime system programming
>
> · Experience in ACE, FIX
>
>
> Could anybody tell me which regex to use to get rid of the dot and the
> leading spaces before each Line?
>
> Thanks for any help!
>
> Jim
>
>
>
This isn't quite right.
There are two ways in which you might use this substitution: either $_
will contain a single line, or it will contain multiple lines. The
single line case might look something like this:
while (<>) {
s/^\.(\s)+//g;
print;
}
In this case, however, the /g modifier is redundant and confusing,
since you only want the regex to match once at the start of each line.
Take the /g modifier off.
The multiple line case might look like this:
$_ = do {local $/; <>;}; # but better to use File::Slurp
s/^\.(\s)+//g;
print;
This code is broken. The problem is that ^ matches start-of-string,
not start-of-line. Therefore, even with the /g modifier the regex can
only match at the beginning of the string, and so will only match
once.
To change ^ to match start-of-line, you need the /m modifier:
s/^\.(\s)+//gm;
print;
Perl Best Practices makes the interesting recommendation that all
regexes should use the /m modifier, because having ^$ match line
boundaries seems to be what most programmers expect and seems to be
useful much more often. If you still need to match start and end of
string you can use \A and \z.
Phil
Cheers,
Parag
I should point out that this may not work. Text in question seems to be
UTF-8. If so, this should be added before any input:
use utf8;
binmode ARGV, ':encoding(utf8)';
--
Just my 0.00000002 million dollars worth,
Shawn
Programming is as much about organization and communication
as it is about coding.
I like Perl; it's the only language where you can bless your
thingy.
You are using capturing parentheses but you are not using the results of
that capture anywhere so why use them? You are using capturing
parentheses in LIST context so even if there are multiple whitespace
characters you are only capturing ONE.
If you are using the parentheses for grouping then it would be more
efficient to use non-capturing parentheses instead, but grouping only
makes sense if you have a GROUP of characters to match, but you only
have ONE.
The use of the /g option is superfluous because the pattern is anchored
to the beginning of the string and there is only ONE beginning of string
in any string.
John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway