Solved

split

Posted on 2000-04-03
5
157 Views
Last Modified: 2010-03-05
How to split the following:

"Quantity_381_04032000101718.x"

to

$a = "381";
and
$b = "04032000101718";

on a nice way???

or split it to:

$a = "381_04032000101718";
0
Comment
Question by:mmcw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 2681122
try this

#!/usr/local/bin/perl
$var="Quantity_381_04032000101718.x";

($junk,$a,$b)=split(/_/,$var);
$b=~ s/\..*//g;

print $a," => ",$b,"\n";
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 2681195
or

($a, $b) = /(\d+)_(\d+)/;

and for the second example:

($a) = /_([\d_]+)/;
0
 

Author Comment

by:mmcw
ID: 2681212
clockwatcher:

Your second answer does not work!!!
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 50 total points
ID: 2681227
$_ = "Quantity_381_04032000101718.x";
($a, $b) = /(\d+)_(\d+)/;
print "$a\n$b\n";

($a) = /_([\d_]+)/;
print "$a\n";

--------------------
Output:
381
04032000101718
381_04032000101718

Which is what you said you wanted.

>> or split it to:
>>  $a = "381_04032000101718";



0
 

Author Comment

by:mmcw
ID: 2681250
Thank you.
I made a mistype
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question