simple regex problem

J

JS

Hi,

I'm trying to build a regex to put the department name into a variable
$dept and the rest of the line into another variable $stats:

E-Test 3 - 4 -
Health and Safety - 1 1 -
Finance - 3 - -

This is my regex:

($dept,$stats)=/^(.[^\s-|\d]*)\s+(.*)/;

but it doesn't work because of the \s- is not actually handled as a
string, but individual charaters.

Can anyone fix this for me please?

Thanks,

JS.
 
J

John W. Krahn

JS said:
I'm trying to build a regex to put the department name into a variable
$dept and the rest of the line into another variable $stats:

E-Test 3 - 4 -
Health and Safety - 1 1 -
Finance - 3 - -

This is my regex:

($dept,$stats)=/^(.[^\s-|\d]*)\s+(.*)/;

but it doesn't work because of the \s- is not actually handled as a
string, but individual charaters.

Can anyone fix this for me please?


my ( $dept, $stats ) = unpack 'A24 A*', $_;



John
 
M

Matija Papec

I'm trying to build a regex to put the department name into a variable
$dept and the rest of the line into another variable $stats:

E-Test 3 - 4 -
Health and Safety - 1 1 -
Finance - 3 - -

This is my regex:

($dept,$stats)=/^(.[^\s-|\d]*)\s+(.*)/;

untested,

my ($dept, $stats) = /(.+?[\d-]+)\s+(.*)/;
 
G

Gunnar Hjalmarsson

John said:
my ( $dept, $stats ) = unpack 'A24 A*', $_;

That doesn't work for the E-Test line. How about:

my ($dept, $stats) = /(.+[a-z])\s+(.*)/;
 
J

JS

John said:
JS said:
I'm trying to build a regex to put the department name into a variable
$dept and the rest of the line into another variable $stats:

E-Test 3 - 4 -
Health and Safety - 1 1 -
Finance - 3 - -

This is my regex:

($dept,$stats)=/^(.[^\s-|\d]*)\s+(.*)/;

but it doesn't work because of the \s- is not actually handled as a
string, but individual charaters.

Can anyone fix this for me please?



my ( $dept, $stats ) = unpack 'A24 A*', $_;



John

Thanks John,

The problem with that is if the dept name is longer than 24 characters e.g:

Customer Service International
 
J

JS

Gunnar said:
John said:
my ( $dept, $stats ) = unpack 'A24 A*', $_;


That doesn't work for the E-Test line. How about:

my ($dept, $stats) = /(.+[a-z])\s+(.*)/;

Thank Gunnar,

That works if I do:

my ($dept, $stats) = /(.+[a-zA-Z])\s+(.*)/;
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

JS said:
Hi,

I'm trying to build a regex to put the department name into a variable
$dept and the rest of the line into another variable $stats:

E-Test 3 - 4 -
Health and Safety - 1 1 -
Finance - 3 - -

This is my regex:

($dept,$stats)=/^(.[^\s-|\d]*)\s+(.*)/;

but it doesn't work because of the \s- is not actually handled as a
string, but individual charaters.

Can anyone fix this for me please?

Not until you explain how you can tell where the department name ends and
the "rest of the line" begins.

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP1xcbWPeouIeTNHoEQLAeQCgufJlY2+TqrfKseQRoKzuJwmmSa8An0yB
JwDum1lkjJ/0GP4V/PI9z6uX
=E/iX
-----END PGP SIGNATURE-----
 
T

Tad McClellan

JS said:
I'm trying to build a regex to put the department name into a variable
$dept and the rest of the line into another variable $stats:

E-Test 3 - 4 -
Health and Safety - 1 1 -
Finance - 3 - -


my($dept, $stats) = split /\s\s+/, $_, 2;
 
T

Tad McClellan

JS said:
The problem with that is if the dept name is longer than 24 characters e.g:


What does the data look like when the dept name is longer than 24 characters?

How can you tell where the longer-than-24-character name ends and
the data in the following column begins?

We do not have enough information to answer your (modified) question.
 
J

John W. Krahn

Gunnar said:
my ( $dept, $stats ) = unpack 'A24 A*', $_;

That doesn't work for the E-Test line. How about:

my ($dept, $stats) = /(.+[a-z])\s+(.*)/;

Sorry, "doesn't work" is not enough information for me to fix the
problem.


John
 
J

John W. Krahn

JS said:
JS said:
I'm trying to build a regex to put the department name into a variable
$dept and the rest of the line into another variable $stats:

E-Test 3 - 4 -
Health and Safety - 1 1 -
Finance - 3 - -

This is my regex:

($dept,$stats)=/^(.[^\s-|\d]*)\s+(.*)/;

but it doesn't work because of the \s- is not actually handled as a
string, but individual charaters.

Can anyone fix this for me please?

my ( $dept, $stats ) = unpack 'A24 A*', $_;

The problem with that is if the dept name is longer than 24 characters e.g:


my @fields = split /(\s{2,})/;
my $dept = shift @fields;
shift @fields;
my $stats = join '', @fields;



John
 
G

Gunnar Hjalmarsson

John said:
Gunnar said:
John said:
my ( $dept, $stats ) = unpack 'A24 A*', $_;

That doesn't work for the E-Test line. How about:

my ($dept, $stats) = /(.+[a-z])\s+(.*)/;

Sorry, "doesn't work" is not enough information for me to fix the
problem.

Didn't know it was your problem. ;-) Anyway, I tested your suggestion
with OP's original example data, i.e. without changing the number of
spaces:

while (<DATA>) {
my ( $dept, $stats ) = unpack 'A24 A*', $_;
print "$dept\n$stats\n\n";
}

__DATA__
E-Test 3 - 4 -
Health and Safety - 1 1 -
Finance - 3 - -

+++++++++++++++++++++++++++
Output:
E-Test 3 -
4 -

Health and Safety
- 1 1 -

Finance
- 3 - -
+++++++++++++++++++++++++++

Suppose that explains it.

As others have pointed out, as long as we don't know for sure how you
safely separate department name from the rest, we are still just guessing.
 

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
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top