Jump to content

Recommended Posts

Posted (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 by NassauSky
Posted

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)

Posted (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 by NassauSky
Posted

@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.

Posted

Hey,

Not a RegExp solution, and no idea if it will fit all your needs, but this works:

Global $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

 

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
  • Recently Browsing   0 members

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