Search code examples
regexsplitnotepad++delimiter

Split line in three words and then rearrange it to create a php array


I have these lines:

 FOO    100     BOR  
 PAL    350     PIL  
 KIL    090     KUP
 FAS    130     FOO's BAR
 FAO    120     Pizza's joy poy

and I need to transform them as following

 array(100, "FOO", "BOR"),
 array(350, "PAL", "PIL"),
 array(090, "KIL", "KUP"),
 array(130, "FAS", "FOO's BAR"),
 array(120, "FAO", "Pizza's joy poy"),

(the words delimiter is a tab)
so far this expression was working fine but it works only two words (not three):

Find : (^.*)    (.*?$)
Replace: array\("\2", \1\)\,

What about these?

 125    My Pot    FAO
 120    Pizza's joy poy  POI

to

array("My Pot", 125, "FAO"),
array("Pizza's joy poy", 120, "POI"),

Is it possible with notepad++ or another editor?


Solution

  • This works

    Find: (?m)^[^\S\r\n]*(\S+)[ \t]+(\S+)[ \t]+(\S+)
    Replace: array($2, "$1", "$3"),

    https://regex101.com/r/xZjcCO/1


    If you have this

    FOO 100 BOR
    PAL 350 PIL
    KIL 090 KUP
    FAS 130 FOO's BAR
    FAO 120 Pizza's joy poy

    change the regex to this (?m)^[^\S\r\n]*(\S+)[ \t]+(\S+)[ \t]+(\S+(?:[^\S\r\n]*\S+)*)

    which trims the whitespace at the end.

    https://regex101.com/r/3d5JTh/1

    output :

    array(100, "FOO", "BOR"),  
    array(350, "PAL", "PIL"),  
    array(090, "KIL", "KUP"),
    array(130, "FAS", "FOO's BAR"),
    array(120, "FAO", "Pizza's joy poy"),  
    

    update

    You can mix phrases as long as there is a tab maintained delimiter :

    (?m)^[^\S\r\n]*(\S+(?:[^\S\t\r\n]*\S+)*)\t+(\S+(?:[^\S\t\r\n]*\S+)*)\t+(\S+(?:[^\S\t\r\n]*\S+)*)

    https://regex101.com/r/CoiGtU/1

    Output

    array(100, "FOO", "BOR"),  
    array(350, "PAL", "PIL"),  
    array(090, "KIL", "KUP"),
    array(130, "FAS", "FOO's BAR"),
    array(120, "FAO", "Pizza's joy poy"),
    
    array(My Pot, "125", "FAO"),
    array(Pizza's joy poy, "120", "POI"),