Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Help with a regex

by Athanasius (Archbishop)
on Jul 24, 2015 at 08:16 UTC ( [id://1136134]=note: print w/replies, xml ) Need Help??


in reply to Help with a regex

Hello mavericknik,

I think you’re looking for something like this:

#! perl use strict; use warnings; while (<DATA>) { print "$1.$2\n" if m{ ^ \s* Pin: \s* ([^.]*?) \. (\S*) (?: \s+ out +)? $ }x; } __DATA__ Pin: U343.IN1 in Pin: U713.INP out Pin: U714.QN

Output:

18:14 >perl 1318_SoPW.pl U713.INP U714.QN 18:14 >

Update: Some notes:

  • The /x modifier makes the regex easier to read.
  • The character classes \s and \S represent whitespace and non-whitespace characters, respectively.
  • There is no need to escape a . inside a character class.
  • The *? quantifier is non-greedy, so if there’s a second . character in the string the literal \. will still match the first one.
  • The (?: ...)? construct matches the contents optionally, and without capturing.

Hope that helps,

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

Replies are listed 'Best First'.
Re^2: Help with a regex
by mavericknik (Sexton) on Jul 24, 2015 at 08:26 UTC
    That is perfect. Thank you very much!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-03-28 19:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found