Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: reg expression question

by healingtao (Novice)
on Jan 29, 2015 at 07:14 UTC ( [id://1114875]=note: print w/replies, xml ) Need Help??


in reply to Re: reg expression question
in thread reg expression question

Thanks very much to all of you who answered. You guys are awesome. I chose Athanasius's answer and everything I anticipated yesterday worked with this solution but there are a few more use cases that came up. Athanasius, While I'm still learning, do you mind updating code for the following additional cases: 1) Input: AB AL0024 Output with code provided: AB- Desired output: AB-AL0024 (if the year is missing, then is it possible to add a dash?) 2) Input: DRSVA 1994 K-2 Output with code provided: DRSVA94- K-2 Desired Output: DRSVA94-K-2 (spaces are not allowed in the output) 3) Input: PUN VALEY B Output with code provided: PUN-VALEY B Desired output: PUN-VALEYB (spaces are not allowed in the output) 4)Input: TIBET 2015 Output with code provided: TIBET15- Desired output: TIBET15 (is it possible to avoid dashes if nothing follows it?) The original cases were critical and these are special cases which will appear rarely if at all but I just wanted to code for it just in case. Let me know if this is possible. Thanks

Replies are listed 'Best First'.
Re^3: reg expression question
by Athanasius (Archbishop) on Jan 29, 2015 at 08:02 UTC

    These special cases are fairly easy to accommodate:

    1. To prevent 4 consecutive digits from being wrongly identified as a year, specify that the digits occur at the start of the right-hand string:

      if (my @m = $right =~ /^\d{2}(\d{2})(.*)/) # ^ Add this

      Within a regex, the special character ^ means “match at the start of the line.”

    2. To remove spaces, use the substitution operator (with the /g modifier for global replacement):

      $right =~ s/\s+//g;
    3. As for 2.

    4. To prevent the string from ending in a dash, use the substitution operator again:

      $right =~ s/-$//;

      $ is another special regex character: it means “match at the end of the line.”

    See “Metacharacters” in perlre.

    Note the value of using a test-driven approach: I was able to add the 4 new input/output pairs to %data, make changes to get the new test cases to pass, and know that these modifications did not invalidate the original solution (because the 4 original test cases still pass).

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        You are right, when used in a regex without an /m modifier, ^ means “match the beginning of the string,” and is equivalent to \A. “Match at the beginning of the line” (which is the definition given in perlre#Regular-Expressions) is strictly correct only when ^ is used in a regex with an /m modifier. So maybe this part of the documentation should be re-worded?

        I haven’t used Perl::Critic, and as to this particular policy — well, I’m suspicious of guidelines that say “always do X” regardless of context. In the present case, adding an /m modifier to the regex would, IMO, be misleading, as it would imply (or at least suggest) that the string being matched is expected to contain multiple newlines. But I acknowledge that this is a judgment call, and YMMV.

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1114875]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-03-30 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found