3 Replies - 8008 Views - Last Post: 06 December 2013 - 10:48 PM

#1 esludds  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 02-July 12

Perl script to extract data using regex

Posted 02 December 2013 - 09:53 AM

Ive got a file containing many email addresses, ip address and dates. I need to write a perl script to read in the file, use a regex expression to detect each of these and output them to an other file.

The reg expressions I have are:

Email =
/[A-Z0-9._%+-][email protected][A-Z0-9.-]+\[A-Z]{2,4}/g


Ip address =
 \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b 


Date =
[0-9]{2}/[Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec]/[0-9]{4} 


Im wondering if these are correct and if someone could help me put them into a perl script.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Perl script to extract data using regex

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13400
  • View blog
  • Posts: 53,476
  • Joined: 12-June 08

Re: Perl script to extract data using regex

Posted 02 December 2013 - 10:01 AM

Quote

Im wondering if these are correct

What happens when you test them?

Example:
http://www.regextester.com/


Quote

if someone could help me put them into a perl script.

What have you tried? What have you looked into?
Was This Post Helpful? 0
  • +
  • -

#3 esludds  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 02-July 12

Re: Perl script to extract data using regex

Posted 02 December 2013 - 12:20 PM

View Postmodi123_1, on 02 December 2013 - 10:01 AM, said:

Quote

Im wondering if these are correct

What happens when you test them?

Example:
http://www.regextester.com/


Quote

if someone could help me put them into a perl script.

What have you tried? What have you looked into?


I've tried this but it wont work it will compile but is just printing that there are no email addresses

#!/usr/bin/perl

open(IN, "<Assignment2.rtf") || die("file not found");
#chooses the file to read
open(OUT, ">emailaddresses.txt");
#prints file

$line = <IN>;
$none = "No email addresses found!";
for ($line)
{
    if ($line =~ /[A-Z0-9._%+-][email protected][A-Z0-9.-]+\[A-Z]{2,4}/g)
    {
        print (OUT $line);
    }
    else
    {
        print (OUT $none);
    }
}

close(IN);
close(OUT);



As for the other regexs they wont compile
Was This Post Helpful? 0
  • +
  • -

#4 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1439
  • View blog
  • Posts: 3,609
  • Joined: 28-November 09

Re: Perl script to extract data using regex

Posted 06 December 2013 - 10:48 PM

They're malformed is the reason. You're assuming all caps, for one. \w is word character, . is any character.

basic regex:
^[email protected]+\.\w+$



That'll get an email address, you should also google email regex as a reference.

http://www.rubular.com
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1