Jump to content
Sign in to follow this  
LurchMan

RegEx Help

Recommended Posts

LurchMan

Hello All -

I've been trying most of the morning to get a regex to work with the section of HTML code below. I basically just want to pull out all the HTML tags and get printer names and the status of the printer below it. I've tried to teach myself the RegEx world but all I can manage is the simple ones. If someone could please show me the pattern to accomplish this and can explain it to me I would appreciate it.

<div class="tableborder">
<table cellspacing="0" cellpadding="0" id="printer_status">
<tr><th colspan="3">Printer Status</th></tr>
<tr>
<th class="second">Printer</th>
<th class="second">Status</th>
<th class="second">Jobs</th>
</tr>
<tr class="normalcolor">
<td>HPL43-Orphan</td>
<td>printer HPL43-Orphan is idle.  enabled since Fri Mar  9 10:47:05 2012</td>
<td>There are no print jobs</td>
</tr>
<tr class="altcolor">
<td>HP1505-Actg</td>
<td>printer HP1505-Actg is idle.  enabled since Fri Mar  9 10:47:30 2012</td>
<td>There are no print jobs</td>
</tr>
<tr class="normalcolor">
<td>HPL41-Invest</td>
<td>printer HPL41-Invest is idle.  enabled since Wed Mar 14 08:19:22 2012</td>
<td>There are no print jobs</td>
</tr>
<tr class="altcolor">
<td>HPL24-GENSVCS</td>
<td>printer HPL24-GENSVCS is idle.  enabled since Fri Mar  9 10:48:25 2012</td>
<td>There are no print jobs</td>
</tr>
<tr class="normalcolor">
<td>HPL42-MK1</td>
<td>printer HPL42-MK1 is idle.  enabled since Mon Oct 18 13:36:33 2010</td>
<td>There are no print jobs</td>
</tr>
<tr class="altcolor">
<td>HP4515-GENSVCS</td>
<td>printer HP4515-GENSVCS is idle.  enabled since Tue Mar 13 15:00:14 2012</td>
<td>There are no print jobs</td>
</tr>
<tr class="normalcolor">
<td>HPL22-TECH</td>
<td>printer HPL22-TECH is idle.  enabled since Fri Mar  9 10:49:26 2012</td>
<td>There are no print jobs</td>
</tr>
<tr class="altcolor">
<td>HPL43-Commis</td>
<td>printer HPL43-Commis is idle.  enabled since Tue Mar 13 13:20:30 2012</td>
<td>There are no print jobs</td>
</tr>
<tr class="normalcolor">
<td>HPL24-Annu5</td>
<td>printer HPL24-Annu5 is idle.  enabled since Tue Mar 13 13:11:43 2012</td>
<td>There are no print jobs</td>
</tr>
</table>
</div>

Edit: Spelling

Edited by LurchMan

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Share this post


Link to post
Share on other sites
guinness

_StringBetween can achieve this, see

#include <Array.au3>
#include <String.au3>

Local $sHTML = '<tr class="normalcolor">' & @CRLF
$sHTML &= '<td>HPL22-TECH</td>' & @CRLF
$sHTML &= '<td>printer HPL22-TECH is idle.  enabled since Fri Mar  9 10:49:26 2012</td>' & @CRLF
$sHTML &= '<td>There are no print jobs</td>' & @CRLF
$sHTML &= '</tr>'
$sHTML &= '<tr class="normalcolor">' & @CRLF
$sHTML &= '<td>HPL22-TECH</td>' & @CRLF
$sHTML &= '<td>printer HPL22-TECH is idle.  enabled since Fri Mar  9 10:49:26 2012</td>' & @CRLF
$sHTML &= '<td>There are no print jobs</td>' & @CRLF
$sHTML &= '</tr>'

Local $aReturn = _StringBetween($sHTML, '<td>', '</td>')
For $i = 0 To UBound($aReturn) - 1
    ConsoleWrite("Printer: " & $aReturn[$i] & @CRLF)
    $i += 2 ; Add 2 because the we don't need the next two values.
Next
_ArrayDisplay($aReturn)

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
Share on other sites
Melba23

LurchMan,

This works for me:

#include <Array.au3>

$sText = '<div class="tableborder">' & @CRLF & _
        '<table cellspacing="0" cellpadding="0" id="printer_status">' & @CRLF & _
        '<tr><th colspan="3">Printer Status</th></tr>' & @CRLF & _
        '<tr>' & @CRLF & _
        '<th class="second">Printer</th>' & @CRLF & _
        '<th class="second">Status</th>' & @CRLF & _
        '<th class="second">Jobs</th>' & @CRLF & _
        '</tr>' & @CRLF & _
        '<tr class="normalcolor">' & @CRLF & _
        '<td>HPL43-Orphan</td>' & @CRLF & _
        '<td>printer HPL43-Orphan is idle.  enabled since Fri Mar  9 10:47:05 2012</td>' & @CRLF & _
        '<td>There are no print jobs</td>' & @CRLF & _
        '</tr>' & @CRLF & _
        '<tr class="altcolor">' & @CRLF & _
        '<td>HP1505-Actg</td>' & @CRLF & _
        '<td>printer HP1505-Actg is idle.  enabled since Fri Mar  9 10:47:30 2012</td>' & @CRLF & _
        '<td>There are no print jobs</td>' & @CRLF & _
        '</tr>' & @CRLF & _
        '<tr class="normalcolor">' & @CRLF & _
        '<td>HPL41-Invest</td>' & @CRLF & _
        '<td>printer HPL41-Invest is idle.  enabled since Wed Mar 14 08:19:22 2012</td>' & @CRLF & _
        '<td>There are no print jobs</td>' & @CRLF & _
        '</tr>' & @CRLF & _
        '<tr class="altcolor">' & @CRLF & _
        '<td>HPL24-GENSVCS</td>' & @CRLF & _
        '<td>printer HPL24-GENSVCS is idle.  enabled since Fri Mar  9 10:48:25 2012</td>' & @CRLF & _
        '<td>There are no print jobs</td>' & @CRLF & _
        '</tr>' & @CRLF & _
        '<tr class="normalcolor">' & @CRLF & _
        '<td>HPL42-MK1</td>' & @CRLF & _
        '<td>printer HPL42-MK1 is idle.  enabled since Mon Oct 18 13:36:33 2010</td>' & @CRLF & _
        '<td>There are no print jobs</td>' & @CRLF & _
        '</tr>' & @CRLF & _
        '<tr class="altcolor">' & @CRLF & _
        '<td>HP4515-GENSVCS</td>' & @CRLF & _
        '<td>printer HP4515-GENSVCS is idle.  enabled since Tue Mar 13 15:00:14 2012</td>' & @CRLF & _
        '<td>There are no print jobs</td>' & @CRLF & _
        '</tr>' & @CRLF & _
        '<tr class="normalcolor">' & @CRLF & _
        '<td>HPL22-TECH</td>' & @CRLF & _
        '<td>printer HPL22-TECH is idle.  enabled since Fri Mar  9 10:49:26 2012</td>' & @CRLF & _
        '<td>There are no print jobs</td>' & @CRLF & _
        '</tr>' & @CRLF & _
        '<tr class="altcolor">' & @CRLF & _
        '<td>HPL43-Commis</td>' & @CRLF & _
        '<td>printer HPL43-Commis is idle.  enabled since Tue Mar 13 13:20:30 2012</td>' & @CRLF & _
        '<td>There are no print jobs</td>' & @CRLF & _
        '</tr>' & @CRLF & _
        '<tr class="normalcolor">' & @CRLF & _
        '<td>HPL24-Annu5</td>' & @CRLF & _
        '<td>printer HPL24-Annu5 is idle.  enabled since Tue Mar 13 13:11:43 2012</td>' & @CRLF & _
        '<td>There are no print jobs</td>' & @CRLF & _
        '</tr>' & @CRLF & _
        '</table>' & @CRLF & _
        '</div>'

$aRet = StringRegExp($sText, "<td>printers(.*).", 3)

_ArrayDisplay($aRet)

Does it for you? :oops:

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

 

Share this post


Link to post
Share on other sites
guinness

Or that as well, I missed the Printer <PrinterName> part.


UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
Share on other sites
LurchMan

Melba and guinness - Thanks for the replies they both work great! guinness's solution is the better one in this case as i need printer name and status for a report I'm generating. Thank you both for your help!

Melba - Quick question for future reference, can you explain this part of the regex: "\s(.*)\."

Thank you both for your quick responses!


Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Share this post


Link to post
Share on other sites
jchd

On behalf of Melba23 (hope you don't mind!).

s stands for "one whitespace" (in the sense of PCRE regexp).

(.*) captures the longest string of characters which doesn't cause the rest of the pattern to fail (by default, the regexp is "greedy")

. stands for a litteral dot (".")

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
Melba23

Lurchman,

Sure:

printers  - Look for the string "printer" followed by a space (s)
(.*)           - Capture (because of the brackets) all the characters until you find
.             - A period - which we have to escape with  as teh SRE engine uses . as a control character

3              - Display an array of all matches found

All clear? :oops:

M23

jchd,

hope you don't mind!

Not at all! Edited by Melba23

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

 

Share this post


Link to post
Share on other sites
LurchMan

Melba and jchd - Thank you for explaining the regex portion of this. I searched the internet and the auto forum trying to figure out to to capture characters when you don't know how many there are and it looks like I was at least close.

Again - everyone, thank you for your help.


Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Share this post


Link to post
Share on other sites
Melba23

LurchMan,

This site is very good for explaining SREs - I still use it a lot. :bye:

jchd recommends this one as well. :doh:

Have fun - they still make my brain bleed! :oops:

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

 

Share this post


Link to post
Share on other sites
jchd

A very good generic tutorial on regexps: http://www.asiteaboutnothing.net/regexp/

The complete PCRE documentation comes with the source tarball at ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ (the PCRE library used in AutoIt may be a couple of PCRE versions behind but that only affects most advanced features or internals).


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
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
Sign in to follow this  

×