Jump to content
VIP

[SOLVED] Insert count in the first element on Array

Recommended Posts

I know your problem is solved, but maybe you can modify your regex to add a blank value at the first position of your returned array.

Here is the trick :

#Include <Array.au3>

$string = "This is a string with seven words"

$aWords = StringRegExp($string, "(?|^()|(\w+))", 3)
$aWords[0] = UBound($aWords) - 1
_ArrayDisplay($aWords)

You can note the empty group ^() which captures an empty value once. It's the first value of the returned array, so you can set it to UBound($array) - 1

How to add a blank value at the first position ?

Local $sArray = StringRegExp("-(A))-(B))-", "(?si)\Q(\E(.*?)(?=\Q)\E)", 3)
;~ $sArray[0] = UBound($sArray) - 1
If IsArray($sArray) Then
;~  For $i = 1 To $sArray[0]
    For $i = 0 To UBound($sArray) - 1
        ConsoleWrite($sArray[$i] & @CRLF)
    Next
Else
    ConsoleWrite("E: " & @error & " > " & $sArray & @CRLF)
EndIf

 

Share this post


Link to post
Share on other sites

@Trong :

Local $sArray = StringRegExp("-(A))-(B))-", "(?si)(?|^()|\Q(\E(.*?)(?=\Q)\E))", 3)

Edit : where did you find the regex ?

Edited by jguinch

Share this post


Link to post
Share on other sites

jguinch,
I was just replying to your post #16 about the generic creation of a blank $array[0] using regex  :)

BTW this regex in post #22 is strictly the one used by _StringBetween() , extracted from the include
It could make sense if  \Q...\E  was needed to escape complicated strings - obviously not the case in the example the OP provided

Share this post


Link to post
Share on other sites

OK, thanks. I usally use \Q..\E, but now, I don't understand why the regexp in #22 works...

You certainly introduced a typo in your copy/paste   ;)

#Include <Array.au3>
;Local $sArray = StringRegExp("-(A))-(B))-", "(?si)\Q(\E(.*?)(?=\Q)\E)", 3)
Local $sArray = StringRegExp("-(A))-(B))-", "(?si)\((.*?)(?=\))", 3)
_ArrayDisplay($sArray)

 

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

×
×
  • Create New...