How to extract expression matching an email address in a text file using R or Command Line?

后端 未结 3 1990
情歌与酒
情歌与酒 2020-12-18 12:18

I have a text file that contains email addresses and some information.

I would like to know how can I extract those email address using R or the terminal?

I\

相关标签:
3条回答
  • 2020-12-18 13:02

    Read your file into R and use grep.

    myText <- readLines("your.file")
    Emails <- grep("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$", myText, value=T)
    

    This will return the whole line that the email appears on, if there is other information on that line you will need to split it up first using something like strsplit

    0 讨论(0)
  • 2020-12-18 13:04

    Lets take an unstructured example file:

    this is a test
    
    fred is fred@foo.com and joe is joe@example.com - but
     @this is a twitter handle for twit@here.com
    

    Then if you do:

    myText <- readLines("testmail.txt")
    emails = unlist(regmatches(myText, gregexpr("([_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4}))", myText)))
    > emails
    [1] "fred@foo.com"    "joe@example.com" "twit@here.com"  
    

    it extracts a vector of all the emails, including when there's more than one on a line. I don't think it will find email addresses broken over line breaks, but if you paste the read lines together it might do that too:

    > myText = paste(readLines("testmail.txt"),collapse=" ")
    > emails = regmatches(myText, gregexpr("([_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4}))", myText))
    > emails
    [[1]]
    [1] "fred@foo.com"    "joe@example.com" "twit@here.com"  
    

    In this case there's only one line in myText because we pasted all the lines together, so there's only one element in the returned list emails object.

    Note that regex string isn't a strict definition of a valid email address. For example, it limits itself to addresses that are between 2 and 4 characters after the last dot. So it doesn't match fred@foo.fnord. There are top level domains that are longer than four characters so you may need to modify the regex.

    Also, it only matches alphanumeric and dot in the name part - so valid addresses such as foo+bar@google.com won't match.

    A regex that fixes these two issues might be:

     "([_+a-z0-9-]+(\\.[_+a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,14}))"
    

    but it probably has other issues and you'd be better of searching for a better email address regex online. I say better, because a perfect one doesn't exist...

    0 讨论(0)
  • 2020-12-18 13:10

    This can also work :

     aa <- paste(readLines("C:\\MY_FOLDER\\NOI\\file1sample.txt"),collapse = " ")
     temp <- sapply(str_extract_all(aa,"[a-z_+0-9]+\\@\\w+\\.[a-z]{2,4}"), function(x){ paste(x,collapse = " ")})
    
    0 讨论(0)
提交回复
热议问题