gcue Posted January 6, 2010 Posted January 6, 2010 hello.there are several lines in a text file i am trying to grab lines that contain a line similar to thisid="CN=Richard Pandy/OU=HR/OU=CG/O=COMPand grab the name (in this case "Richard Pandy") that falls in the between of id="CN= and /OU=HR/OU=CG/O=COMPthe /OU=HR/OU=CG/O=COMP changes character length and wording.tricky!!!!!thanks in advance
Moderators Melba23 Posted January 6, 2010 Moderators Posted January 6, 2010 gcue, Does this work for you? $sString = 'id="CN=Richard Pandy/OU=HR/OU=CG/O=COMP' $sName = StringRegExpReplace($sString, '(?i)id="CN=([\w|\x20]*).*', '$1') ConsoleWrite($sName & @CRLF) M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
gcue Posted January 6, 2010 Author Posted January 6, 2010 not quite.. sorry i didnt mention theres tons of text before and afterid="CN=Richard Pandy/OU=HR/OU=CG/O=COMP
Xenobiologist Posted January 6, 2010 Posted January 6, 2010 If the text before and after changes, then we need some examples to find a pattern that matches all of them. Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times
gcue Posted January 6, 2010 Author Posted January 6, 2010 (edited) <user communityId="Sametime_1150897993593.1840414137" displayName="Bob Rhen/CDS/CG/COMP" id="CN=Bob Rhen/OU=CDS/OU=CG/O=COMP" lastChat="1258398577039"/><user communityId="Sametime_1150897993593.1840414137" displayName="Nel Augie" id="CN=Nel Augie/OU=CDS/OU=CG/O=COMP"/> <user communityId="Sametime_1150897993593.1840414137" displayName="Troy Tompkins (employment verification)" id="CN=Troy Tompkins/OU=HR/OU=CG/O=COMP"/>thank you! Edited January 6, 2010 by gcue
Xenobiologist Posted January 6, 2010 Posted January 6, 2010 #include <Array.au3> $str = '<user communityId="Sametime_1150897993593.1840414137" displayName="Bob Rhen/CDS/CG/COMP" id="CN=Bob Rhen/OU=CDS/OU=CG/O=COMP" lastChat="1258398577039"/>' & @CRLF & _ '<user communityId="Sametime_1150897993593.1840414137" displayName="Nel Augie" id="CN=Nel Augie/OU=CDS/OU=CG/O=COMP"/>' & @CRLF & _ '<user communityId="Sametime_1150897993593.1840414137" displayName="Troy Tompkins (employment verification)" id="CN=Troy Tompkins/OU=HR/OU=CG/O=COMP"/>' & @CRLF & _ 'id="CN=Richard Pandy/OU=HR/OU=CG/O=COMP' $re = StringRegExp($str, 'id="CN=(.*?)/', 3) _ArrayDisplay($re) Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times
Xenobiologist Posted January 6, 2010 Posted January 6, 2010 No prob. Could also be done this way. #include <String.au3> #include <Array.au3> $str = '<user communityId="Sametime_1150897993593.1840414137" displayName="Bob Rhen/CDS/CG/COMP" id="CN=Bob Rhen/OU=CDS/OU=CG/O=COMP" lastChat="1258398577039"/>' & @CRLF & _ '<user communityId="Sametime_1150897993593.1840414137" displayName="Nel Augie" id="CN=Nel Augie/OU=CDS/OU=CG/O=COMP"/>' & @CRLF & _ '<user communityId="Sametime_1150897993593.1840414137" displayName="Troy Tompkins (employment verification)" id="CN=Troy Tompkins/OU=HR/OU=CG/O=COMP"/>' & @CRLF & _ 'id="CN=Richard Pandy/OU=HR/OU=CG/O=COMP' $re = _StringBetween($str, 'id="CN=', '/') _ArrayDisplay($re) Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times
gcue Posted January 11, 2010 Author Posted January 11, 2010 thanks again for your help gentlemen - it's working great. id like to see if i can make this a bit more effective. when it finds an entry that matches the condition, id like to grab the catagory that name falls under here's me show you an example.. <group expanded="false" name="Payroll" type="private"> <user communityId="Sametime_1150897993593.1840414137" displayName="Mike Davis/HR/CG/COMP" id="CN=Mike Davis/OU=HR/OU=CG/O=COMP"/> <user communityId="Sametime_1150897993593.1840414137" displayName="Jennifer Conal/HR/CG/COMP" id="CN=Jennifer Conal/OU=HR/OU=CG/O=COMP" lastChat="1256926651392"/> <user communityId="Sametime_1150897993593.1840414137" displayName="Justin Godard" id="CN=Justin Godard/OU=CA/OU=CG/O=COMP" lastChat="1255550532658"/> <user communityId="Sametime_1150897993593.1840414137" displayName="Jon Randall/HR/CG/COMP" id="CN=Jon Randall/OU=HR/OU=CG/O=COMP"/> </group> <group expanded="false" name="Accounts Payable" type="private"> <user communityId="Sametime_1150897993593.1840414137" displayName="Francis Zep" id="CN=Francis Zep/OU=CA/OU=CG/O=COMP"/> <user communityId="Sametime_1150897993593.1840414137" displayName="Tina Amos" id="CN=Tina Amos/OU=CA/OU=CG/O=COMP"/> <user communityId="Sametime_1150897993593.1840414137" displayName="Evan Jackson/CA/CG/COMP" id="CN=Evan Jackson/OU=CA/OU=CG/O=COMP" lastChat="1256925203314"/> <user communityId="Sametime_1150897993593.1840414137" displayName="Chaz Martinez" id="CN=Chaz Martinez/OU=CA/OU=CG/O=COMP"/> </group> so if Chaz Martinez is a match thats found, id like to also report "Accounts Payable" here's what i have #include <File.au3> #include <array.au3> #include <Clipboard.au3> $file = FileOpenDialog("Please choose XML file", @DesktopDir, "XML (*.xml)", 3) If @error Then Exit EndIf Dim $buddies, $clipboard, $buddy_list[1][1] _FileReadToArray($file, $buddies) For $x = 1 To UBound($buddies) - 1 $name = StringRegExp($buddies[$x], 'id="CN=(.*?)/', 3) For $y = 0 To UBound($name) - 1 $search = _ArraySearch($buddy_list, $name[$y]) If @error Then ReDim $buddy_list[UBound($buddy_list) + 1][2] $buddy_list[UBound($buddy_list) - 1][0] = $name[$y] $buddy_list[UBound($buddy_list) - 1][1] = "eh" $clipboard &= $name[$y] & @LF Else MsgBox(262144, "FYI", $name[$y] & " is duplicated on your contact list.") EndIf Next Next _ClipBoard_SetData($clipboard) _ArrayDisplay($buddy_list)
Moderators SmOke_N Posted January 11, 2010 Moderators Posted January 11, 2010 I don't think "eh" qualifies as an actual effort. Lucky ( or unlucky if I didn't understand your goal ) I was interested in the string manipulation.expandcollapse popup#include <Array.au3> #Region Testing Purpose Only Global $sz_string = "" $sz_string &= '<group expanded="false" name="Payroll" type="private">' & @CRLF $sz_string &= '<user communityId="Sametime_1150897993593.1840414137" displayName="Mike Davis/HR/CG/COMP" id="CN=Mike Davis/OU=HR/OU=CG/O=COMP"/>' & @CRLF $sz_string &= '<user communityId="Sametime_1150897993593.1840414137" displayName="Jennifer Conal/HR/CG/COMP" id="CN=Jennifer Conal/OU=HR/OU=CG/O=COMP" lastChat="1256926651392"/>' & @CRLF $sz_string &= '<user communityId="Sametime_1150897993593.1840414137" displayName="Justin Godard" id="CN=Justin Godard/OU=CA/OU=CG/O=COMP" lastChat="1255550532658"/>' & @CRLF $sz_string &= '<user communityId="Sametime_1150897993593.1840414137" displayName="Jon Randall/HR/CG/COMP" id="CN=Jon Randall/OU=HR/OU=CG/O=COMP"/>' & @CRLF $sz_string &= '<user communityId="Sametime_1150897993593.1840414137" displayName="Chaz Martinez" id="CN=Chaz Martinez/OU=CA/OU=CG/O=COMP"/>' & @CRLF $sz_string &= '</group>' & @CRLF $sz_string &= '<group expanded="false" name="Accounts Payable" type="private">' & @CRLF $sz_string &= '<user communityId="Sametime_1150897993593.1840414137" displayName="Francis Zep" id="CN=Francis Zep/OU=CA/OU=CG/O=COMP"/>' & @CRLF $sz_string &= '<user communityId="Sametime_1150897993593.1840414137" displayName="Tina Amos" id="CN=Tina Amos/OU=CA/OU=CG/O=COMP"/>' & @CRLF $sz_string &= '<user communityId="Sametime_1150897993593.1840414137" displayName="Evan Jackson/CA/CG/COMP" id="CN=Evan Jackson/OU=CA/OU=CG/O=COMP" lastChat="1256925203314"/>' & @CRLF $sz_string &= '<user communityId="Sametime_1150897993593.1840414137" displayName="Chaz Martinez" id="CN=Chaz Martinez/OU=CA/OU=CG/O=COMP"/>' & @CRLF $sz_string &= '</group>' $sz_string &= '<group expanded="false" name="Accounts Receivable" type="private">' & @CRLF $sz_string &= '<user communityId="Sametime_1150897993593.1840414137" displayName="Joseph Zep" id="CN=Joeseph Zep/OU=CA/OU=CG/O=COMP"/>' & @CRLF $sz_string &= '<user communityId="Sametime_1150897993593.1840414137" displayName="Mike Davis" id="CN=Mike Davis/OU=CA/OU=CG/O=COMP"/>' & @CRLF $sz_string &= '<user communityId="Sametime_1150897993593.1840414137" displayName="Michael Jackson/CA/CG/COMP" id="CN=Michael Jackson/OU=CA/OU=CG/O=COMP" lastChat="1256925203314"/>' & @CRLF $sz_string &= '<user communityId="Sametime_1150897993593.1840414137" displayName="Chaz Martinez" id="CN=Chaz Martinez/OU=CA/OU=CG/O=COMP"/>' & @CRLF $sz_string &= '</group>' Global $s_xml_file = $sz_string #EndRegion Testing Purpose Only #cs ; Uncomment and remove region above when ready to test on live file Global $s_xml_file = FileOpenDialog("Please choose XML file", @DesktopDir, "XML (*.xml)", 3) If @error Then Exit 1 EndIf #ce Global $a_clients = _XML_ClientGetGroups($s_xml_file) If @error Then Exit 2 _ArrayDisplay($a_clients) Func _XML_ClientGetGroups($s_xml) Local $s_string = $s_xml If FileExists($s_xml) Then $s_string = FileRead($s_xml) ; Separate the individual groups Local $s_group_pattern = "(?i)(?s)" $s_group_pattern &= '(<group expanded=".*?" name=".*?" type="private">.*?</group>)' Local $a_groups = StringRegExp($s_string, $s_group_pattern, 3) If @error Then Return SetError(1, 0, 0) ; Create a unique array of all the names Local $a_names, $a_ret_names[1] = [0], $i_ret_add, $i_ub For $i_group = 0 To UBound($a_groups) - 1 $a_names = StringRegExp($a_groups[$i_group], "(?i)\x22CN=(.*?)/", 3) If @error Then ContinueLoop $i_ub = UBound($a_names) ReDim $a_ret_names[$i_ub + $a_ret_names[0] + 1] $a_ret_names[0] += $i_ub For $i_name = 0 To $i_ub - 1 $i_ret_add += 1 $a_ret_names[$i_ret_add] = $a_names[$i_name] Next Next If $i_ret_add = 0 Then Return SetError(2, 0, 0) ; Get the unique names out of all the array values Local $a_names_unique = _ArrayUnique($a_ret_names, 1, 1) ; Reloop through list [n][0] will have name [n][nn] will have group they belong to Local $i_elements = 0, $a_find, $i_index Local $s_find_pattern = '(?i)(?s)<group expanded=".*?" name="(.*?)"' Local $a_ret[$a_names_unique[0] + 1][1] = [[$a_names_unique]] For $i_name = 1 To $a_names_unique[0] $a_ret[$i_name][0] = $a_names_unique[$i_name] $i_index = 0 For $i_group = 0 To UBound($a_groups) - 1 If StringInStr($a_groups[$i_group], '"CN=' & $a_names_unique[$i_name] & "/") = 0 Then ContinueLoop EndIf $a_find = StringRegExp($a_groups[$i_group], $s_find_pattern, 1) If @error Then ContinueLoop $i_index += 1 ; Increase 2nd dimension if need be If $i_index > $i_elements Then $i_elements = $i_index ReDim $a_ret[$a_names_unique[0] + 1][$i_elements + 1] EndIf $a_ret[$i_name][$i_index] = $a_find[0] Next Next Return $a_ret EndFuncMaybe it's time for you to set time aside to learn how regex works and can work for you... having said that, I hope not to see another regex thread from you that shows no effort what so ever on making it work before you post a support question. Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
gcue Posted January 12, 2010 Author Posted January 12, 2010 works great! thank you know of a good regex tutorial?
nfwu Posted January 12, 2010 Posted January 12, 2010 works great!thank youknow of a good regex tutorial?neogia's old guide was handy: http://www.autoitscript.com/forum/index.php?showtopic=23627"technical" document if you're into that stuff: http://www.autoitscript.com/autoit3/pcrepattern.html TwitterOut of date stuff:Scripts: Sudoku Solver | Webserver | 3D library (Pure AutoIt) | Wood's GadgetsUDFs: _WoodUniqueID() | _DialogEditIni() | _Console*() | _GetIPConfigData() | _URLEncode/Decode()
PsaltyDS Posted January 12, 2010 Posted January 12, 2010 (edited) A new favorite of mine (don't remember who linked it to this forum originally): Regular-Expressions.infoEdit: What the heck...? The link URL is being mangled by the forum editor (remove spaces): www . regular - expressions . info Edited January 12, 2010 by PsaltyDS Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
gcue Posted January 12, 2010 Author Posted January 12, 2010 thanks for the links! psalty, the link isnt working
PsaltyDS Posted January 12, 2010 Posted January 12, 2010 thanks for the links!psalty, the link isnt working The forum editor mangled it. But the URL is same as the site name (without spaces): www . regular - expressions . info Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now