• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

Regular Expression

I am new to regex and need to do the following:

From a text file I have rows with 2 columns of text as follows:

name directory
name directory
name directory

I know the name and need to search the file for the name and return the directory associated with the name. I know it is very simple using regex but I am done racking my brain.
1 Solution

open(IN,"./mail.txt") || die $!;
$name="this is a file";

foreach (@mails){
  if (/^$name (.*)/){
print $dir,"\n";

this is a file /www/sbarney/temp
test /www/sbarney
this+_ir that /temp

NOTE: i have assumed that the filename can contain spaces too.
Also i have hardcoded the file name in the $name var in the perl program.
$name = "name";
while (<>)
  print "$1\n" if /^$name (.*)$/;

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now