PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 28, 2022

[FIXED] How to rearrange this csv email name list

 August 28, 2022     csv, email     No comments   

Issue

I have a .csv list of emails + names. Each name can have 1-3 emails along with it (which are currently separated by a comma). I need to convert this into a .csv list where its 1 name and 1 email.

Here is example:

John Smith,johnsmith1@gmail.com,johnsmith2@gmail.com,johnsmith3@gmail.com
Taylor Smith,taylorsmith@gmail.com
Jack Smith,jacksmith1@gmail.com,jacksmith@gmail.com
...(and there are like 10k more rows)

How can I automatically convert this to:

John Smith,johnsmith1@gmail.com
John Smith,johnsmith2@gmail.com
John Smith,johnsmith3@gmail.com
Taylor Smith,taylorsmith@gmail.com
Jack Smith,jacksmith1@gmail.com
Jack Smith,jacksmith@gmail.com

The main problem here attaching the name to separate rows of the emails that were intially with that name.

I appreciate any help - seems like an easy task, but was stuck on this for few days already, Thanks.


Solution

If you can use Notepad++ here is a two steps solution:

Replace > Options: Check • Regular expressions, Uncheck [ ] . matches newline

  1. Rearrange line -> Move Name to end of the line
    Search for ^([^,@\n]+),(.+)\R? and replace with $2,$1\n
    Step 1 > demo at regex101 (explanation on the right side)

  2. Now the Name can be captured inside a lookahead and used as a replacement.
    Search for ([^\n,]++)(?=.*,([^\n,@]+$)),(?:(?1)(?:\n|$))? and replace with $2,$1\n
    Step 2 > demo at regex101

    Each comma seperated substring gets captured by the first group while the second group captures the Name inside a lookahead and is put before the E-Mail and removed from the end.

After both steps the result should look like your desired outcome. Further info: SO Regex FAQ



Answered By - bobble bubble
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing