regular expressions

S

Sokar

hi,

I have the string

$variable = ",1,2,3,4,5";

I was wondering if anyone knew a regular expression that would remove the
first "," so that I would have

$variable = "1,2,3,4,5"

Thanks and regards
 
G

Gunnar Hjalmarsson

Sokar said:
I have the string

$variable = ",1,2,3,4,5";

I was wondering if anyone knew a regular expression that would remove the
first "," so that I would have

$variable = "1,2,3,4,5"

You would have known, if you had studied e.g.

perldoc perlrequick

But if all you want to do is removing the first character, the substr()
function is sufficient.
 
P

Peroli

hi tony,
with regard to your code:
s/,//;

That will remove all the commas, leaving you with string "12345"

$variable = ",1,2,3,4,5";
$varaible =~ s/^,+//g;
will remove any number of leading commas.

Peroli Sivaprakasam
 
A

A. Sinan Unur

hi tony,
with regard to your code:
s/,//;

That will remove all the commas, leaving you with string "12345"

$variable = ",1,2,3,4,5";
$varaible =~ s/^,+//g;
will remove any number of leading commas.

Hmmmm ...

C:\Home\asu1\UseNet\clpmisc> type crap.pl
use strict;
use warnings;

my $variable = ",1,2,3,4,5";
$varaible =~ s/^,+//g;

print $variable;

C:\Home\asu1\UseNet\clpmisc> crap.pl
Global symbol "$varaible" requires explicit package name at C:\Home\asu1
\UseNet\clpmisc\crap.pl line 5.
Execution of C:\Home\asu1\UseNet\clpmisc\crap.pl aborted due to
compilation errors.

Please make sure your code is valid before you post. The easiest way to
catch this mistake would have been to make sure you had

use strict;

in your script as the posting guidelines recommend.

Second point: You are only removing a leading sequence of commas. By
definition, there can only be one leading sequence of commas. Why do you
think you need the g option above?

use strict;
use warnings;

my $v = ",,,,,,,,,,,,,,,,,,,1,2,3,4,5";
$v =~ s/^,+//;

print $v;

__END__

C:\Home\asu1\UseNet\clpmisc> crap.pl
1,2,3,4,5

Sinan
 
S

Scott Bryce

Peroli said:
hi tony,
with regard to your code:
s/,//;

That will remove all the commas, leaving you with string "12345"

Are you sure?

s/,//g;

will remove all of the commas.

s/,//;

will remove the first comma found, even if it isn't the first character.
 
T

Tore Aursand

Peroli said:
with regard to your code:
s/,//;

That will remove all the commas, leaving you with string "12345"

No. s/,// will remove the _first_ comma in the variable. I really don't
think the OP want to do that; I think the OP just want to remove the
comma if it appears as the first character in the variable;

s/^,//;

The expression s/,//g; will remove _all_ the commas in the variable, by
the way.
 
P

Peroli

hi tony,
with regard to your code:
s/,//;
That will remove all the commas, leaving you with string "12345"
$variable = ",1,2,3,4,5";
$varaible =~ s/^,+//g;
will remove any number of leading commas.

My apologies for not testing the script before posting.

s/,//;
will remove only one comma. I was mislead by overconfidence. And
finally
$varaible =~ s/^,+//g;
"g" option is not needed too.

Peroli Sivaprakasam
 
T

Tad McClellan

Sokar said:
I have the string

$variable = ",1,2,3,4,5";

I was wondering if anyone knew a regular expression that would remove the
first "," so that I would have

$variable = "1,2,3,4,5"


Show us the code you have so far, and we will help you fix it.
 
L

Lars Eighner

In our last episode,
the lovely and talented Sokar
broadcast on comp.lang.perl.misc:
I have the string
$variable = ",1,2,3,4,5";
I was wondering if anyone knew a regular expression that would remove the
first "," so that I would have

Yes, there are many, many ways of doing that as the first few
follow-ups have shown. It is pointless to discuss regular
expressions in a specific case and you haven't described a
general case well enough. Do you mean:

a) remove the first character in a string,
b) remove the first character in a string if it is a comma,
c) remove the first comma in a string (whether it precedes
another character or not),
d) remove the first comma in a string only if it precedes a
numeral,
e) some other possibile interpretations of your requirements I
haven't thought of.

and

Do you know the 1) the comma you want removed will always be
there or 2) the comma you want removed may or may not be there?
 
B

Bart Lateur

Sokar said:
I have the string

$variable = ",1,2,3,4,5";

I was wondering if anyone knew a regular expression that would remove the
first "," so that I would have

$variable = "1,2,3,4,5"

You already have the solutions to your immediate problem, so I won't go
there... But I was wondering how come you end up with such a string.
Could it be you have constructed that string in a loop? Something like
this:

@n = (1, 2, 3, 4, 5);
$variable = "";
foreach (@n) {
$variable .= ",$_";
}


In such a case, I can tell you that perl has a better way: using join.

$variable = join ",", @n;


No leading comma, you'll end up with the end result you want.
 
A

Anno Siegel

Lars Eighner said:
In our last episode,
the lovely and talented Sokar
broadcast on comp.lang.perl.misc:
I have the string
$variable = ",1,2,3,4,5";
I was wondering if anyone knew a regular expression that would remove the
first "," so that I would have
[...]

general case well enough. Do you mean:

a) remove the first character in a string,
b) remove the first character in a string if it is a comma,
c) remove the first comma in a string (whether it precedes
another character or not),
d) remove the first comma in a string only if it precedes a
numeral,
e) some other possibile interpretations of your requirements I
haven't thought of.

and

Do you know the 1) the comma you want removed will always be
there or 2) the comma you want removed may or may not be there?
$variable = "1,2,3,4,5"

Thanks for the analysis.

An innocent, but carelessly worded question like the one up there can
open up a vast tree of alternatives. They all must be considered before
you can even begin to think about a solution. This is how the time
spent in answering is sometimes inverse exponential in proportion to
the time spent on stating the question. It can get annoying...

Anno
 
I

Ian Wilson

Sokar said:
hi,

I have the string

$variable = ",1,2,3,4,5";

I was wondering if anyone knew a regular expression that would remove the
first "," so that I would have

$variable = "1,2,3,4,5"

Tell the group which regular expressions you have tried. They'll then be
able to help you better.

Have you read the FAQ and tried command "perldoc perlre"?
 

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

Forum statistics

Threads
474,166
Messages
2,570,907
Members
47,446
Latest member
Pycoder

Latest Threads

Top