Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Regex: matching any Number then a hyphen

by Anonymous Monk
on Nov 27, 2022 at 12:50 UTC ( [id://11148404]=perlquestion: print w/replies, xml ) Need Help??

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

I have lines of data, all begin with a number. For lines which have a " - " or " – " after the number, but before the next piece of text I'd like to replace this with a ". ". example data
1234 - Foo 56778 – Bar 9999. Baz
desired output
1234. Foo 56778. Bar 9999. Baz

Replies are listed 'Best First'.
Re: Regex: matching any Number then a hyphen
by 1nickt (Canon) on Nov 27, 2022 at 15:42 UTC

    Hi,

    What did you try? How did it fail?

    use strict; use warnings; use utf8; use Test::More tests => 3; for my $line(<DATA>) { chomp $line; my ($string, $wanted) = split '#', $line; $string =~ s/^(\d+)\s*[–-](.*)/$1.$2/; is $string, $wanted; } __END__ 1234 - Foo#1234. Foo 56778 – Bar#56778. Bar 9999. Baz#9999. Baz
    Output:
    1..3 ok 1 ok 2 ok 3

    Hope this helps!


    The way forward always starts with a minimal test.
Re: Regex: matching any Number then a hyphen
by Anonymous Monk on Nov 27, 2022 at 13:32 UTC
    here u go u lazy bum
    perl -CSD -pe 's/^\d+\K\s*[-.\x{2013}]/./'
    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://11148404]
Approved by 1nickt
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found