CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
REGEXP not capturing $1 target

 



stuckinarut
User

Sep 6, 2014, 7:13 AM

Post #1 of 5 (13352 views)
REGEXP not capturing $1 target Can't Post

I've tried umpteen combinations of stuff but can't seem to find what is wrong in the following REGEXP to capture the $1 target of ([0-9A-Z\/]+) which will ALWAYS start at position 33 in the line:


Code
m/^[\s]{32}([0-9A-Z\/]+)[\s\n\r\t]+/


NOTE: For some reason the "." before the last \s will NOT display in this posting

Here is one data line format from the input file with the target being Q9XRX/P:


Code
29.08.2014 1658    94832 XQR    Q9XRX/P           91     37 PXRU


The target may vary from 4 to 12 [0-9A-Z] with *possibly* a [\/] involved.

Thanks for any help!

-stuckinarutFrown


(This post was edited by stuckinarut on Sep 6, 2014, 7:35 AM)


FishMonger
Veteran / Moderator

Sep 6, 2014, 7:40 AM

Post #2 of 5 (13346 views)
Re: [stuckinarut] REGEXP not capturing $1 target [In reply to] Can't Post

Why do you start the pattern with this?

Code
^[\s]{32}

Clearly you don't understand what that matches. It says that you expect the string to begin with 32 whitespace characters, which it obviously doesn't. BTW, the brackets are an unnecessary redundancy in this case. I'll let you lookup the regex documentation to learn why.

There are multiple ways (both with or without a regex) to extract that field, here's one regex option.

Code
/^.{32}(\S+)/



stuckinarut
User

Sep 6, 2014, 8:04 AM

Post #3 of 5 (13343 views)
Re: [FishMonger] REGEXP not capturing $1 target [In reply to] Can't Post

Hello, FishMonger:

My REGEXP did have a (.) = PERIOD before the \s in both cases, but these would NOT display in my posting or editing ;-(

When I could not get the actual REGEXP to work, I added the [] brackets.

I had researched some REGEXP documentation, but obviously failed to comprehend everything.

Thanks for your help (again).

-stuckinarut


FishMonger
Veteran / Moderator

Sep 6, 2014, 8:31 AM

Post #4 of 5 (13338 views)
Re: [stuckinarut] REGEXP not capturing $1 target [In reply to] Can't Post

This is the book you need. Mastering Regular Expressions, 3rd Edition
http://shop.oreilly.com/product/9780596528126.do


(This post was edited by FishMonger on Sep 6, 2014, 8:31 AM)


Laurent_R
Veteran / Moderator

Sep 6, 2014, 10:22 AM

Post #5 of 5 (13333 views)
Re: [stuckinarut] REGEXP not capturing $1 target [In reply to] Can't Post

The regex suggested by Fishmonger should definitely solve your problem. However since your data format is essentially positional, it might be better to use the substr function or possibly the unpack function. At least it will most probably be significantly faster (this may be important if you are processing a large quantity of data).

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives