Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Perl Regex

by audioboxer (Acolyte)
on Nov 17, 2019 at 05:12 UTC ( [id://11108809]=perlquestion: print w/replies, xml ) Need Help??

audioboxer has asked for the wisdom of the Perl Monks concerning the following question:

Hello! I am reading through cells in a excel file and trying to capture data through a regular expression however I am having two issues;
1.) I get error "Use of ?PATTERN? without explicit operator is deprecated"
2.) I can't figure out how to also get instances where there is a space within what I am looking for (Scenario 2).

regex: my @matches = ( my $str =~ (?:link:) [^\s\|]+ );

Input Data:
Scenario 1
Link:1625 housing Link:2004

Scenario 2
Link:638 92-5000|Link:63892-5070

Desired Output Data:
1625
2004
638 92-5000
63892-5070

I appreciate the help, thank you.

Replies are listed 'Best First'.
Re: Perl Regex
by AnomalousMonk (Archbishop) on Nov 17, 2019 at 06:12 UTC

    Here's another approach:

    c:\@Work\Perl\monks>perl -wMstrict -le "print qq{perl version $]}; ;; my @strings = ( 'Link:1625 housing Link:2004', 'Link:638 92-5000|Link:63892-5070', ); ;; my $rx_numeric = qr{ \d+ (?: - \d+)? }xms; ;; for my $str (@strings) { my @matches = $str =~ m{ Link : ($rx_numeric (?: \s+ $rx_numeric)?) + }xmsg; print qq{'$str' -> }, map qq{'$_' }, @matches; } " perl version 5.008009 'Link:1625 housing Link:2004' -> '1625' '2004' 'Link:638 92-5000|Link:63892-5070' -> '638 92-5000' '63892-5070'
    Add the  /i modifier if you want case insensitive matching:
        my @matches = $str =~ m{ ... }xmsgi;
    For  qr// see Regexp Quote-Like Operators in perlop. For  =~ see Binding Operators in perlop. See also perlre, perlretut, and perlrequick.

    Update: I often find it prudent to add an extra element of safety by using boundary assertions in data field regexes. So  $rx_numeric might be better defined as (tested)
        my $rx_numeric = qr{ (?<! \d) \d+ (?: - \d+)? (?! \d) }xms;
    See  (?<!pattern) (?!pattern) in Extended Patterns in perlre.


    Give a man a fish:  <%-{-{-{-<

Re: Perl Regex
by swl (Parson) on Nov 17, 2019 at 05:30 UTC

    Edited about 30 secs after posting - the regex does not work.

    Your regex is not delimited. Using the explicit match (m//) notation might help clarify things.

    It also needs the g modifier to match all cases.

    my $str = 'Link:1625 housing Link:2004'; my @matches = ( $str =~ m/(?:link:) [^\s\|]+/g );

    See perlretut for more examples.

    (I have not tested that the regex works, though).

      Try this for the regex. It works with the test data provided.

      my @matches = ( $str =~ m/\|?Link:([\s0-9-]+)/g );
Re: Perl Regex
by BillKSmith (Monsignor) on Nov 19, 2019 at 23:03 UTC
    Think of this pattern as a string of digits followed by any number of a sub-patterns consisting of a separator and more digits.
    use strict; use warnings; use feature 'state'; use Test::More tests => 2; my @scenarios = ( # String Expected match ["Link:1625 housing Link:2004", ['1625', '2004' ]], ["Link:638 92-5000|Link:63892-5070", ['638 92-5000', '63892-5070']], ); my $pattern = qr/\d+ (?: [- ]\d+ )* /x; foreach my $scenario (@scenarios) { state $i = 1; my $str = $scenario->[0]; my @matches = $str =~ m/Link: ($pattern)/gx; is_deeply( \@matches, $scenario->[1], "Scenario $i"); $i++; }
    OUTPUT: 1..2 ok 1 - Scenario 1 ok 2 - Scenario 2
    Bill
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11108809]
Approved by marto
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-19 08:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found