Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: How to write a regular expression to extract a number from an embedded string?

by pbeckingham (Parson)
on Jul 28, 2004 at 16:42 UTC ( [id://378105]=note: print w/replies, xml ) Need Help??


in reply to How to write a regular expression to extract a number from an embedded string?

How about this?

my $string = 'abcdef 5163 1234 5678'; my $extracted = $string =~ /(5163(?:\d{8}|\s\d{4}\s\d{4}))/;
Update: modified code. Thanks davido

  • Comment on Re: How to write a regular expression to extract a number from an embedded string?
  • Download Code

Replies are listed 'Best First'.
Re^2: How to write a regular expression to extract a number from an embedded string?
by davido (Cardinal) on Jul 28, 2004 at 20:24 UTC

    This solution makes the assumption that if spaces occur, they will always occur at four-digit intervals. The OP didn't say that was the case. While it may be the case, we're not sure.

    Your use of tr/// is ineffective, at any rate. It's doing nothing. If evaluated in scalar context it would return the number of spaces found, but if you intended to delete spaces, you need to add the /d modifier. However, that would be contrary to what the OP was asking. In his example output, whitespace is preserved.


    Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-23 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found