Jump to content

regexp help


gcue
 Share

Recommended Posts

hello.

there are several lines in a text file

i am trying to grab lines that contain a line similar to this

id="CN=Richard Pandy/OU=HR/OU=CG/O=COMP

and grab the name (in this case "Richard Pandy") that falls in the between of id="CN= and /OU=HR/OU=CG/O=COMP

the /OU=HR/OU=CG/O=COMP changes character length and wording.

tricky!!!!!

thanks in advance

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<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 by gcue
Link to comment
Share on other sites

#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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)
Link to comment
Share on other sites

  • Moderators

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.

#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
EndFunc
Maybe 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.

Link to comment
Share on other sites

works great!

thank you

know 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

Link to comment
Share on other sites

A new favorite of mine (don't remember who linked it to this forum originally): Regular-Expressions.info

:D

Edit: What the heck...? The link URL is being mangled by the forum editor (remove spaces): www . regular - expressions . info

Edited 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
Link to comment
Share on other sites

thanks for the links!

psalty, the link isnt working :huggles:

The forum editor mangled it. But the URL is same as the site name (without spaces): www . regular - expressions . info

:D

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
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...