Jump to content

Searching for Strings in Text Using Wildcards


Go to solution Solved by sahsanu,

Recommended Posts

I'm hoping someone can help me with this.

I'm new to Autoit and I'm stuck on how to achieve the following. It looks like it should be easy, but I've drawn a blank so far.

Below is an example taken from an automatically generated .xml file that's sent to a client computer when booted up on a network. What I need to do is search for the string between the "MachineName" quotes and assign it to a variable to use in a script that will insert it into several places in the Windows registry. The last 4 characters are always the same, but the first 5 characters are always different.

-<NODES>
   -<NODE>
      <MachineName>A1234BCDE</MachineName>

This is what I mean in English, if that makes sense: -

$computername = The result of searching for "(5 wildcards)BCDE" in the .xml file so that, in the example above, it will be "A1234BCDE" - no more and no less.

I'm sure I can work out the rest of the script once I've got past this.

Many thanks in advance,

Steve.

Link to comment
Share on other sites

  • Moderators

Rincewind,

Welcome to the AutoIt forum. :)

You need a Regular Expression - but the _StringBetween function will do all the hard work for you:

#include <String.au3>

$sString = "-<NODES>" & @CRLF & _
    "-<NODE>" & @CRLF & _
    "       <MachineName>A1234BCDE</MachineName>"

$aRet = _StringBetween($sString, "<MachineName>", "</MachineName>")

ConsoleWrite($aRet[0] & @CRLF)
All clear? :)

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

Rincewind,

Welcome to the AutoIt forum. :)

You need a Regular Expression - but the _StringBetween function will do all the hard work for you:

#include <String.au3>

$sString = "-<NODES>" & @CRLF & _
    "-<NODE>" & @CRLF & _
    "       <MachineName>A1234BCDE</MachineName>"

$aRet = _StringBetween($sString, "<MachineName>", "</MachineName>")

ConsoleWrite($aRet[0] & @CRLF)
All clear? :)

M23

 

Thanks for the quick response. It kind of makes sense. Sorry for being a bit thick, but I'm confused about the #include <string.au3> bit - I'm assuming this is what you mean by "Regular Expression". Do I have to download "string.au3" from somewhere?

Link to comment
Share on other sites

Using XMLDOM:

#include <File.au3>
; create file as an example
$file = @DesktopDir & "\temp.xml"
_FileCreate($file)
FileWrite($file,"<something><NODES><NODE><MachineName>1</MachineName></NODE><NODE><MachineName>2</MachineName></NODE><NODE><MachineName>3</MachineName></NODE><NODE><MachineName>A1234BCDE</MachineName></NODE><NODE><MachineName>4</MachineName></NODE></NODES></something>")
$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load($file)
$oNODES = $oXML.selectNodes("//NODES/NODE")

For $oNODE In $oNODES
    $oMachineName = $oNODE.selectSingleNode("./MachineName")
    ConsoleWrite($oMachineName.text & @CRLF)
Next

output:

1
2
3
A1234BCDE
4

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

  • Moderators

Rincewind,

String.au3 is one of the "include files" that were installed along with AutoIt. These files contain functions written in AutoIt itself which expand the functionality of the language - by "including them you effectively add this code to your script. The _StringBetween function is part of the String.au3 include file - if you comment out the #include line you will see that AutoIt throws an "undefined function" error as it cannot find the function.

A "Regular Expression" is a separate thing altogether. The _StringBetween function uses one of them to do its magic, but there is actually an SRE engine inside AutoIt to do the hard work. SREs are very useful, but quite daunting when you first start learning about them - they make my brain bleed quite often. I recommend this site if you want to learn more about them.

Does that answer your questions? :)

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

Rincewind,

String.au3 is one of the "include files" that were installed along with AutoIt. These files contain functions written in AutoIt itself which expand the functionality of the language - by "including them you effectively add this code to your script. The _StringBetween function is part of the String.au3 include file - if you comment out the #include line you will see that AutoIt throws an "undefined function" error as it cannot find the function.

A "Regular Expression" is a separate thing altogether. The _StringBetween function uses one of them to do its magic, but there is actually an SRE engine inside AutoIt to do the hard work. SREs are very useful, but quite daunting when you first start learning about them - they make my brain bleed quite often. I recommend this site if you want to learn more about them.

Does that answer your questions? :)

 

Not immediately, but I'm a lot wiser than I was a short while ago. ;-)

 

To give you more of an idea of the purpose of the script, we use client Workstations that have an application that needs to connect to a database server. In order to avoid the long winded process of installing the client whilst connected to the server, which would do all of configuration as part of the installation process, we use preinstalled clients, where the only difference between the networks on which they're used is the name of the database server. At the moment, we instruct installers to get the name of the server from the file. write it down on a bit of paper and use a utility to do a "find and replace" on the client registry. The problem with all this is, it's down to me to do the documentation and training, and spend hours on the phone trying to sort it out when a mistake is made. If it can be automated, it means I get a (slightly) easier life. :-)

 

Many thanks for all the suggestions. I'll go away and have a play. It all looks a bit daunting at the moment but I'm sure, like most things I've had to learn, it'll come to me in a flash of inspiration - usually, inconveniently, in the middle of the night. ;-(

Edited by Melba23
Fixed quote tags
Link to comment
Share on other sites

  • Moderators

Rincewind,

Glad the fog is beginning to lift. Never feel that a question is "too easy" to ask - we all started as complete beginners. Just make sure you have really searched the help file and the forum before posting. ;)

 

like most things I've had to learn, it'll come to me in a flash of inspiration

Not SREs, I can almost guarantee that! After some years I am still waiting for the "blinding flash" - much to the amusement of the real SRE gurus around here. :D

M23

P.S. When you reply, please use the "Reply to this topic" button at the top of the thread or the "Reply to this topic" editor at the bottom rather than the "Quote" button - I know what I wrote and it just pads the thread unneccessarily. ;)

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

I've got a bit further with this, but I'm stuck on the following and I can't understand why. It looks logical to me. :-(

I'm having trouble with the variable "$Name" in the code below.
I've read the manual and searched the Internet, but I can't see what I'm doing wrong.

The following code works fine and I get the server name displayed in a box correctly, but when I substitute the code that after "; Display Servername in a box" with any of the two examples underneath, the output is as if the variable is blank.

;Read Servername from Hosts.xml
#include <Array.au3>
#include <String.au3>
$file = FileOpen("Hosts.xml")
$Filecontent = FileRead($file)
$Name = _StringBetween($Filecontent, "Server", "-->")
; Display Servername in a box
_ArrayDisplay($Name, "Server Name")

1
; Send Servername to registry key.
RegWrite("HKEY_CURRENT_USERSoftwareTest", "Test", "REG_SZ", $Name)

2
; Send Servername to text file.
$TextFile = FileOpen("Hosts.txt")
FileWrite($TextFile, $Name)

I only actually need No.1 to work, but I can't understand why No2 also doesn't work. The both give an output as if the string value is zero.

I'm writing and testing the code on a Windows 8.1 64 bit machine, but intend to use it on a Win2K machine.

TIA

Steve.

 

Link to comment
Share on other sites

  • Solution

Hello,

_StringBetween returns an array so $Name is an array and you should explicit wrote what row from that array you want to write to registry, file, whatever...

For example, if you want to use the first row in the array:

RegWrite("HKEY_CURRENT_USER\Software\Test", "Test", "REG_SZ", $Name[0])

Take a look to Arrays section in the help file.

Edit: Added link to Autoit Wiki (Arrays)

Edited by sahsanu
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...