Extract text from a string with tabs

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Didier3L
Posts: 14
Joined: 27 Dec 2016, 09:33

Extract text from a string with tabs

24 Jan 2017, 07:40

Bonjour,
Je cherche à extraire du texte d'une chaine de caractère du presse papier délimité par des tabulations ?

Hello,
I'm trying to extract text from a character string in the tab-delimited clipboard?

Contents of the paper press :

Code: Select all

Jean	Dupont	10 Rue des roses	75016	Paris
I would like to retrieve in a variable each element
prenom = Jean
nom = Dupont
adresse = 10 Rue des roses
CP = 75016
ville = Paris

How can I do ?
garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: Extract text from a string with tabs

24 Jan 2017, 07:59

example for text file / or use clipboard ( instead of variable aac )

Code: Select all

FileSelectFile, FL1, 3,%a_desktop%, Choose text file:,Supported Files (*.txt;*.csv)
If FL1 =
  ExitApp
f2=%a_scriptdir%\NEW.txt
ifexist,%f2%
  filedelete,%f2%
fileread,aac,%fl1%              ;- read txt-file to memory
Loop,parse,aac,`n,`r            ;- parse each line by `n ascii-10 (EndOfLine ascii 13,10 )
  {
  x:= a_loopfield
  if x=
     continue
  stringsplit,C,x,`t
    e .= "prenom=" . c1 . ";Nom=" . c2 . ";Adresse=" . c3 . ";CP=" . c4 . ";Ville=" . c5 . "`r`n"
  }
fileappend,%e%,%f2%
e=
run,%f2%
exitapp
User avatar
F4Jonatas
Posts: 45
Joined: 22 Oct 2015, 06:35
Contact:

Re: Extract text from a string with tabs

25 Jan 2017, 18:48

Code: Select all

MsgBox, % Main( "Jean	Dupont	10 Rue des roses	75016	Paris" ).Adresse
ExitApp

Main( String ) {
	Local Result := {}
	Local Keys := [ "Prenom", "Nom", "Adresse", "CP", "Ville" ]
	Local LoopField := ""
	Local Count := 1

	While ( Count := RegExMatch( String, "i)([^\t]+)", LoopField, Count + StrLen( LoopField ) )) {
		Result[ Keys[ A_Index ] ] := LoopField
	}

	Return Result
}

Tell me if it worked.
User avatar
Xeo786
Posts: 760
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: Extract text from a string with tabs

27 Jan 2017, 06:29

I am totally blank at regex so two simple way come to my mind :)

Code: Select all

H_array := ["prenom","nom","adresse","CP","ville"]
clipboard := "Jean	Dupont	10 Rue des roses	75016	Paris" ; remove this line to access live clipboard

ArryClip:=[] 
for a, b in StrSplit(clipboard, "`t") ;1st way 
	ArryClip.Insert(b)

MainClip := {}
Loop, parse, clipboard, %a_tab% ; 2nd way
	MainClip[ H_array[ A_Index ] ] := A_LoopField

msgbox % MainClip.adresse "`n|||OR|||`n" ArryClip[3]
clipboard =
return 
its up to you how would you like to achive your goal :thumbup:
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
User avatar
jmeneses
Posts: 524
Joined: 28 Oct 2014, 11:09
Location: Catalan Republic

Re: Extract text from a string with tabs

27 Jan 2017, 08:28

Another way

Code: Select all

xString := "Jean	Dupont	10 Rue des roses	75016	Paris" 
RegExMatch(xString,"^(?P<Prenom>\w+)\t(?P<Nom>\w+)\t(?P<adresse>\S.+\w+)\t(?P<CP>\d.+)\t(?P<ville>\w.+)$",My)
Msgbox % "prenom" A_Tab "(" MyPrenom ")`nnom" A_Tab "(" MyNom  ")`nadresse" A_Tab "(" Myadresse 
. ")`nCP" A_Tab "(" MyCP ")`nVille" A_Tab "(" MyVille ")"
Donec Perficiam
User avatar
F4Jonatas
Posts: 45
Joined: 22 Oct 2015, 06:35
Contact:

Re: Extract text from a string with tabs

31 Jan 2017, 07:01

This way was faster, but creates global variables.
jmeneses wrote:Another way

Code: Select all

xString := "Jean	Dupont	10 Rue des roses	75016	Paris" 
RegExMatch(xString,"^(?P<Prenom>\w+)\t(?P<Nom>\w+)\t(?P<adresse>\S.+\w+)\t(?P<CP>\d.+)\t(?P<ville>\w.+)$",My)
Msgbox % "prenom" A_Tab "(" MyPrenom ")`nnom" A_Tab "(" MyNom  ")`nadresse" A_Tab "(" Myadresse 
. ")`nCP" A_Tab "(" MyCP ")`nVille" A_Tab "(" MyVille ")"
While this does not create a global or local variable.
F4Jonatas wrote:

Code: Select all

MsgBox, % Main( "Jean	Dupont	10 Rue des roses	75016	Paris" ).Adresse
ExitApp

Main( String ) {
	Local Result := {}
	Local Keys := [ "Prenom", "Nom", "Adresse", "CP", "Ville" ]
	Local LoopField := ""
	Local Count := 1

	While ( Count := RegExMatch( String, "i)([^\t]+)", LoopField, Count + StrLen( LoopField ) )) {
		Result[ Keys[ A_Index ] ] := LoopField
	}

	Return Result
}

Xeo786. Your code did not work as well :roll:
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Extract text from a string with tabs

31 Jan 2017, 07:26

Using a parsing loop:

Code: Select all

q::
Clipboard := "Jean	Dupont	10 Rue des roses	75016	Paris"
Loop, Parse, Clipboard, %A_Tab%
{
(A_Index = 1) ? (prenom := A_LoopField) : ""
(A_Index = 2) ? (nom := A_LoopField) : ""
(A_Index = 3) ? (adresse := A_LoopField) : ""
(A_Index = 4) ? (CP := A_LoopField) : ""
(A_Index = 5) ? (ville := A_LoopField) : ""
}

MsgBox % prenom "`r`n" nom "`r`n" adresse "`r`n" CP "`r`n" ville
Return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Hugh Jars, Mateusz53, MrDoge, peter_ahk and 382 guests