Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: $1 and regex

by tmiklas (Hermit)
on Aug 27, 2002 at 07:22 UTC ( [id://193089]=note: print w/replies, xml ) Need Help??


in reply to $1 and regex

If I understand you well, your input line looks like '3256 nothing special 356216'. In this case it's enough to match digit, non-digit and digit again...

#!/usr/bin/perl while (<>) { print if m/(\d+)\D+(\d+)/; print "$1\n$2\n"; }
The output is:
tm@norad:~$ ./regex_test 123 testing... testing 456 123 testing... testing 456 123 456
UPDATE: after reading next replies...
  1. .+ isn't the best notation - it shouldn't catch next digits after (\d+) but it's not specially safe AIMO, especially makes the code more difficoult to understand
  2. this 'to', whitespaces or other words you mentioned in your problem are covered with \D+ which IMvHO is more universal becouse of catching anything except digits

Greetz, Tom.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-25 05:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found