mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2009-03-01, 02:21   #1
ixfd64
Bemusing Prompter
 
ixfd64's Avatar
 
"Danny"
Dec 2002
California

28×32 Posts
Default regular expression help

I'm having a bit of trouble with regular expressions in R.

I know the following regular expression

Code:
'^.*([-A-Za-z0-9_.%]+@[-A-Za-z0-9_.%]+\\.[A-Za-z]+).*$'
is supposed to return e-mail addresses using backreferences.

However,

Quote:
emailpat = '^.*([-A-Za-z0-9_.%]+@[-A-Za-z0-9_.%]+\\.[A-Za-z]+).*$'
gsub(emailpat, '\\1', 'adadasd>aaaaaaabbbbbbb@cccc.com<test')
returns

Quote:
[1] "b@cccc.com"
and cuts off the first part of the e-mail except for the first letter. Does anyone know what I'm doing wrong?

Thanks.
ixfd64 is offline   Reply With Quote
Old 2009-03-01, 03:33   #2
wblipp
 
wblipp's Avatar
 
"William"
May 2003
New Haven

235810 Posts
Default

I've run into different definitions of regular expressions from time to time. But assuming your situation is like described here:

http://www.regular-expressions.info/reference.html

your problem is the the first ".*" is greedy, trying the longest possible matches first. I think you need to make it lazy, changing the .* to .*?

Code:
emailpat = '^.*?([-A-Za-z0-9_.%]+@[-A-Za-z0-9_.%]+\\.[A-Za-z]+).*$'
If your environment doesn't support lazy, then I'd suggest a "not a regular character" before the "one or more regular characters and outside the parenthesis. so

Code:
emailpat = '^.*[^-A-Za-z0-9_.%]([-A-Za-z0-9_.%]+@[-A-Za-z0-9_.%]+\\.[A-Za-z]+).*$'
good luck

William
wblipp is offline   Reply With Quote
Old 2009-03-01, 06:19   #3
ixfd64
Bemusing Prompter
 
ixfd64's Avatar
 
"Danny"
Dec 2002
California

28×32 Posts
Default

The first one didn't work but the second did. Thanks so much!
ixfd64 is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Expanse has to be the most epic 45 minutes to get a first showing on regular cable. jasong jasong 25 2016-01-21 16:03
meaning of regular flashes of hot lead wildrabbitt Hardware 8 2015-06-22 10:29
Regular polygon: ruler & compass possible only if Raman Puzzles 21 2009-12-09 20:25
Regular expressions (or DFA?) CRGreathouse Math 3 2009-12-09 19:56
Using regular Prime 95 on 64 bit windows? Speedbump Information & Answers 1 2009-07-25 00:51

All times are UTC. The time now is 18:26.

Wed Sep 30 18:26:22 UTC 2020 up 20 days, 15:37, 0 users, load averages: 1.78, 1.73, 1.73

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.