BluGeni
BluGeni

Reputation: 3454

regular expression to replace lastname, firstname middle initial to email format

Need a little help creating a regular expression to take, for example :

Smith, John R 

and turn it into

[email protected]

Upvotes: 0

Views: 753

Answers (4)

Nishant
Nishant

Reputation: 55866

Same as previous answer, but in Shell:

echo "Smith, John R" | awk '{print tolower($0)}' | sed 's/\(.*\),\s\(.*\)\s\(.*\)/\2.\3.\[email protected]/g'
[email protected]

Actually thanks to @KP6, I realized sed can lowercase too :) So, much simpler version would be:

echo "Smith, John R" | sed 's/\(.*\),\s\(.*\)\s\(.*\)/\L\2.\L\3.\L\[email protected]/g'
[email protected]

Upvotes: 1

gwillie
gwillie

Reputation: 1899

Just capture the needed tokens:

(.+?),\s(.+?)\s(.+)

$1 is last name.

$2 is first name

$3 is middle name

Now build your email address.

As mention by other, regex seems like overkill

Upvotes: 0

PKumar
PKumar

Reputation: 11128

Since the language is not specified , i tried this in VIM and it works perfectly.

%s/\v(\w*),\s*(\w*)\s*(\w)/\L\2.\L\3.\[email protected]/

Attached is screenshot

enter image description here

Upvotes: 2

herohuyongtao
herohuyongtao

Reputation: 50667

You can use the following regex in C++11:

string s = "Smith, John R"; // to [email protected]

const regex r("(.*), (.*) (.*)");  
const string fmt("[email protected]");  

cout << regex_replace(s, r, fmt) << endl;  

Note: this will give you [email protected], you may further need to change it to lowercase if you need [email protected], which is quite a easy task.

Upvotes: 2

Related Questions