Strip first few characters form a string

D

DB

I want to remove all characters from a string which come before the
first hyphen. For example I want AB-CDE-FGH to become CDE-FGH Can
anyone suggest a way to do that with perl?

DB
 
M

Matt Garrish

DB said:
I want to remove all characters from a string which come before the
first hyphen. For example I want AB-CDE-FGH to become CDE-FGH Can
anyone suggest a way to do that with perl?

$string =~ s/^[^-]+-//;

Matt
 
J

Jürgen Exner

DB said:
I want to remove all characters from a string which come before the
first hyphen. For example I want AB-CDE-FGH to become CDE-FGH

Your example does not match your description.
Can anyone suggest a way to do that with perl?

TIMTOWTDI:
s/.*-/-/; #matches your description
s/.*-//; #matches your example
(undef, $_) = split /-/, $_, 2; # matches your example
substr($_, 0, index ($_, '-'), ''); #matches your description
substr($_, 0, index ($_, '-')+1, ''); #matches your example

I am sure others will come up with more possibilities.

jue
 
X

Xaonon

Jürgen Exner said:
Your example does not match your description.


TIMTOWTDI:
s/.*-/-/; #matches your description
s/.*-//; #matches your example

ITYM:

s/^.*?-/-/;
s/^.*?-//;

since the * is normally greedy. Or perhaps:

s/^[^-]*-/-/;
s/^[^-]*-//;
 
J

Joe Smith

Jürgen Exner said:
s/.*-/-/; #matches your description
s/.*-//; #matches your example

The first returns "-FGH" and the second returns "FGH" because * is greedy.

s/.*?-/-/; # Remove up to but no including first hyphen
s/.*?-//; # Remove first hyphen and everything before it.

-Joe
 
J

Jürgen Exner

Joe said:
The first returns "-FGH" and the second returns "FGH" because * is
greedy.

Oooops, you are right.
But
s/[^-]*//;
s/[^-]*-//;
should work.

jue
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top