Ask a Question related to PERL Miscellaneous, Design and Development.
-
JS #1
simple regex problem
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.
JS Guest
-
simple regex
I've got a file with a list of ip's in brackets, fqdn's and -'s. How do you simply remove the 's and the -'s from the file with a simple perl... -
"Simple" regex advice (possibly '/' causing an issue?)
Ian.H wrote: (snipped) You are trying to use Perl's $_ but it does not exist, for your context. -
[PHP] regex problem
Merlin wrote: Are you making this too hard? if($string = 'test/contact.html') { echo 'good'; } else { echo 'bad'; } ?? -
Simple regex question
William S. Perrin wrote: You can try this: m/(\w+\b)\s+(+?)\s*/g (untested) -- Gunnar Hjalmarsson -
Simple Regex question (~[^newline characters])
<? // Test example: echo '<pre>'; $aInput = ' abtgh kdghg 9867 jkuhkh ^ { ooijj 438764 - 56_ 67 '; $aInput = ereg_replace('', ',' ,... -
John W. Krahn #2
Re: simple regex problem
JS wrote:
>
> 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
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
Matija Papec #3
Re: simple regex problem
On Mon, 08 Sep 2003 10:35:00 +0100, JS <vervoom@hotmail.com> wrote:
untested,>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+(.*)/;
my ($dept, $stats) = /(.+?[\d-]+)\s+(.*)/;
Matija Papec Guest
-
Gunnar Hjalmarsson #4
Re: simple regex problem
John W. Krahn wrote:
That doesn't work for the E-Test line. How about:> JS wrote:>>> 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 ) = unpack 'A24 A*', $_;
my ($dept, $stats) = /(.+[a-z])\s+(.*)/;
--
Gunnar Hjalmarsson
Email: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]
Gunnar Hjalmarsson Guest
-
JS #5
Re: simple regex problem
John W. Krahn wrote:
Thanks John,> JS wrote:
>>>>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
The problem with that is if the dept name is longer than 24 characters e.g:
Customer Service International
JS Guest
-
JS #6
Re: simple regex problem
Gunnar Hjalmarsson wrote:
Thank Gunnar,> John W. Krahn wrote:
>>>> JS wrote:
>>>>>>> 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 ) = unpack 'A24 A*', $_;
>
> That doesn't work for the E-Test line. How about:
>
> my ($dept, $stats) = /(.+[a-z])\s+(.*)/;
>
That works if I do:
my ($dept, $stats) = /(.+[a-zA-Z])\s+(.*)/;
JS Guest
-
Eric J. Roode #7
Re: simple regex problem
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
JS <vervoom@hotmail.com> wrote in news:bjhihj$nsq$1@cspc1n11.baplc.com:
Not until you explain how you can tell where the department name ends and> 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?
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+TqrfKseQRoKzuJw mmSa8An0yB
JwDum1lkjJ/0GP4V/PI9z6uX
=E/iX
-----END PGP SIGNATURE-----
Eric J. Roode Guest
-
Tad McClellan #8
Re: simple regex problem
JS <vervoom@hotmail.com> wrote:
> 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;
--
Tad McClellan SGML consulting
[email]tadmc@augustmail.com[/email] Perl programming
Fort Worth, Texas
Tad McClellan Guest
-
Tad McClellan #9
Re: simple regex problem
JS <vervoom@hotmail.com> wrote:
> 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.
--
Tad McClellan SGML consulting
[email]tadmc@augustmail.com[/email] Perl programming
Fort Worth, Texas
Tad McClellan Guest
-
John W. Krahn #10
Re: simple regex problem
Gunnar Hjalmarsson wrote:
Sorry, "doesn't work" is not enough information for me to fix the>
> John W. Krahn wrote:>> > JS wrote:> >> >> 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 ) = unpack 'A24 A*', $_;
> That doesn't work for the E-Test line. How about:
>
> my ($dept, $stats) = /(.+[a-z])\s+(.*)/;
problem.
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
John W. Krahn #11
Re: simple regex problem
JS wrote:
>
> John W. Krahn wrote:>> > JS wrote:
> >> >> >>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
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
Gunnar Hjalmarsson #12
Re: simple regex problem
John W. Krahn wrote:
Didn't know it was your problem. ;-) Anyway, I tested your suggestion> Gunnar Hjalmarsson wrote:>>>John W. Krahn wrote:
>>>>>>>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.
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.
--
Gunnar Hjalmarsson
Email: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]
Gunnar Hjalmarsson Guest
-
Buck #13
Re: simple regex problem
On Mon, 08 Sep 2003 10:35:00 +0100, JS wrote:
I know its not a regex, but with variable dept name lenghts, why not:> 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:
while (<DATA>) {
my $f = 4;
my @x = split /\s+/;
my ($dept,$stats) = ( join(" ",@x[0 .. ($#x-$f)]), join(" ",@x[-($f) .. -1]) );
print "Dept: $dept\tStat: $stats\n";
}
__DATA__
E-Test 3 - 4 -
Health and Safety - 1 1 -
Finance - 3 - -
Dept: E-Test Stat: 3 - 4 -
Dept: Health and Safety Stat: - 1 1 -
Dept: Finance Stat: - 3 - -
budman
Buck Guest



Reply With Quote

