NassauSky Posted November 18, 2020 Share Posted November 18, 2020 (edited) I'm having a problem trying to grab the last word on each string. Any regex experts out there? #Include <array.au3> $sValue1 = '<a class="comm-nav-item na-button" href="/d/info" id="2" data-time="14:25;a">Music</a>' $Xpath = StringRegExp($sValue1, '(?i)<([a-zA-Z]\w*)(.*?)[>\t](.*)$|<',3 ) If Ubound($Xpath) = 0 Then MsgBox(0,"Notice", "No results found") Else _ArrayDisplay($Xpath) EndIf $sValue1 = '<button class="comm-nav-item na-button" data-time="14:25;a"> More' $Xpath = StringRegExp($sValue1, '(?i)<([a-zA-Z]\w*)(.*?)[>\t](.*)$|<',3 ) If Ubound($Xpath) = 0 Then MsgBox(0,"Notice", "No results found") Else _ArrayDisplay($Xpath) EndIf I'm looking for the the word Music in the first string and the word More in the second string. Edited November 18, 2020 by NassauSky Link to comment Share on other sites More sharing options...
Marc Posted November 18, 2020 Share Posted November 18, 2020 Something like this? #Include <array.au3> $sValue1 = '<a class="comm-nav-item na-button" href="/d/info" id="2" data-time="14:25;a">Music</a>' $Xpath = StringRegExp($sValue1, ">\s*(\w+)(?:<|$)",1) If Ubound($Xpath) = 0 Then MsgBox(0,"Notice", "No results found") Else _ArrayDisplay($Xpath) EndIf $sValue1 = '<button class="comm-nav-item na-button" data-time="14:25;a"> More' $Xpath = StringRegExp($sValue1, ">\s*(\w+)(?:<|$)",1) If Ubound($Xpath) = 0 Then MsgBox(0,"Notice", "No results found") Else _ArrayDisplay($Xpath) EndIf Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL) Link to comment Share on other sites More sharing options...
NassauSky Posted November 18, 2020 Author Share Posted November 18, 2020 (edited) @Marc I needed both results. The first group which included the html tag: 'a' and 'button' as well as the text after the tag closure: 'Music' and 'More' and it doesn't work for something like this which has a space within the innertext Sign In: $sValue1 = '<span class="slds-truncate" data-aura-rendered-by="2:56;a">Sign In</span>' Edited November 18, 2020 by NassauSky Link to comment Share on other sites More sharing options...
Nine Posted November 18, 2020 Share Posted November 18, 2020 (edited) $Xpath = StringRegExp($sValue1, '^<([^\s]*).*>(.*)(?:<|$)',1) Seems to work alright Edited November 18, 2020 by Nine remove Caseless “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
NassauSky Posted November 18, 2020 Author Share Posted November 18, 2020 @Nine Thanks not quite there yet. $sValue1 = '<a class="comm-nav-item na-button" href="/d/info" id="2" data-time="14:25;a">Music</a>' ;Should result in a 2 element array ["a","Music"] $sValue1 = '<button class="comm-nav-item na-button" data-time="14:25;a"> More' ;Should result in a 2 element array ["button","More"] $sValue1 = '<span class="slds-truncate" data-aura-rendered-by="2:56;a">Sign In</span>' ;Should result in a 2 element array ["span","Sign In"] None of the results above work completely. Either they aren't a resulting array or they don't take into account a possible space or 2 in the last capture group. Thanks for the help so far. Link to comment Share on other sites More sharing options...
Nine Posted November 18, 2020 Share Posted November 18, 2020 Better ? $Xpath = StringRegExp($sValue1, '^<([^\s]*).*?>\s*(.*?)(?:<|$)',1) NassauSky 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
GokAy Posted November 18, 2020 Share Posted November 18, 2020 Hey, Not a RegExp solution, and no idea if it will fit all your needs, but this works: expandcollapse popupGlobal $sValue1 = '<a class="comm-nav-item na-button" href="/d/info" id="2" data-time="14:25;a">Music</a>' test($sValue1,1,"<"," ") test($sValue1,-1,">","<") $sValue1 = '<button class="comm-nav-item na-button" data-time="14:25;a"> More' test($sValue1,1,"<"," ") test($sValue1,-1,">","<") $sValue1 = '<span class="slds-truncate" data-aura-rendered-by="2:56;a">Sign In</span>' test($sValue1,1,"<"," ") test($sValue1,-1,">","<") ; Direction: 1 (Search from left), -1 (Search from Right) func test($s_String, $i_Direction, $s_1stCondition, $s_2ndCondition) Local $i_1stPos = StringInStr($s_String,$s_1stCondition,Default,$i_Direction) Local $s_Word Local $i_Pos_Condition = ($i_Direction = 1) ? (1) : (StringLen($s_String)) if $i_1stPos < $i_Pos_Condition Then $s_Word = StringStripWS(StringRight($s_String,StringLen($s_String)-$i_1stPos),3) Else Local $i_2ndPos = StringInStr($s_String, $s_2ndCondition, Default, $i_Direction) $i_Direction = ($i_Direction = 1) ? (1) : (-2) $i_1stPos = StringInStr($s_String, $s_1stCondition, Default, $i_Direction) $s_Word = StringStripWS(StringMid($s_String, $i_1stPos + 1,($i_2ndPos - $i_1stPos - 1)), 3) endif ConsoleWrite("Word: " & $s_Word & @CRLF) EndFunc Link to comment Share on other sites More sharing options...
NassauSky Posted November 18, 2020 Author Share Posted November 18, 2020 Thanks @GokAy I have a few other ways to solve the problem but I'd rather use regex but thanks again Link to comment Share on other sites More sharing options...
NassauSky Posted November 18, 2020 Author Share Posted November 18, 2020 2 hours ago, Nine said: Better ? $Xpath = StringRegExp($sValue1, '^<([^\s]*).*?>\s*(.*?)(?:<|$)',1) Jackpot Thanks @Nine I almost missed that. Link to comment Share on other sites More sharing options...
Nine Posted November 18, 2020 Share Posted November 18, 2020 1 minute ago, NassauSky said: I almost missed that Ya I was wondering “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
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