Hello again everyone!
I always end up finding myself coming straight back to PerlMonks when I'm stumped because everyone is always so helpful!
Right now I'm attempting to analyze a text file. This text file (I will give an example) with multiple "sections" divided by "\n" characters. These sections contain a bunch of data I don't care about, but there a few lines that I need to grab. Here is where my problem lies.
Example Text File
First Name: John Last Name: Doe Occupation: Network Administrator Location: West Coast First Name: Jane Last Name: Doe Occupation: Human Resources Location: East Coast First Name: James Last Name: Doe Occupation: Technical Support Engineer Location: Central USA
I have been trying to use regex to search for a string, for example "Central USA" and then use that to match the "First Name" and "Last Name" lines and CAPTURE their names.
I attempted to use a regex "lookbehind" but I can't do that since my capture has to be variable in length. I believe this is because I don't know the length of the first or last name and I have to account for that. I have been attempting to use http://regexstorm.net/tester to accomplish this.
When I don't use a lookbehind, my regex search picks up the first "First Name" and "Last Name" line in the file regardless of if it is near where I matched the "Location" field. This makes sense, but I want it to grab the "First Name" and "Last Name" line that came right before "Central USA".
Should I be going at this a different way?
Example Code
if ($line =~ /First Name:\s+([A-Za-z0-9 _ ( )]*).*?Last Name:\s+([A-Za +-z0-9 _ ( )]*).*?Location: Central USA/s) { print $line; }
As always, any help would be greatly appreciated!
In reply to Storing String from Line Before Regex Match by Nico
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |