Jump to content

StringRegExp. How to return text found by predefined variable?


Suppir
 Share

Recommended Posts

Hello!

I have variable

$Name = "[A-Z]\.\s*[A-Z][a-z]+"

Then I use it in regular expression

$Found = StringRegExp($Line, "^(.+)\s" & $Name, 1)

How can I get the text, that was found by predefined variable $Name?

***

The text that was found by the group (.+) is in $Found[0].

But where is the text found by $Name?

Edited by Suppir
Link to comment
Share on other sites

Hello!

I have variable

$Name = "[A-Z]\.\s*[A-Z][a-z]+"

Then I use it in regular expression

$Found = StringRegExp($Line, "^(.+)\s" & $Name, 1)

How can I get the text, that was found by predefined variable $Name?

Hi,

because you use flag 1 for StringRegExp () you get an array as return.

Some ways for the goal:

; After line $Found = for both solutions

;Solution 1
If @error Then
    MsgBox (0,"", "No matches found!")
Else
    For $i = 0 To UBound ($Found) - 1
        MsgBox (0,"", $Found [$i]) ; instead of msgbox code what you want
    Next
EndIf

;Solution 2
#include <array.au3> ; add to the beginning of your code for Solution 2

If @error Then
    MsgBox (0,"", "No matches found!")
Else
    _ArrayDisplay ($Found)
EndIf

;-))

Stefan

Edited by 99ojo
Link to comment
Share on other sites

Stefan, thank you, but it is not exactly what I meaned.

In your solution we are getting element(s) that was(were) captured in this place "^(.+)\s"

But I need a fragment that was found by variable $Name.

To do this I need to set the group (...) on variable $Name.

But it does not work.

In Perl I can say:

$Name = "[A-Z]\.\s*[A-Z][a-z]+";

print $2 if /^(.+)\s($Name)/

You see, here is two groups in regular expression. And "print $2" will print some fragment, that found in second parenthesis ($Name).

How to do it in AutoIt? Thanks

Edited by Suppir
Link to comment
Share on other sites

It's the same as doing something odd:

Opt("ExpandVarStrings", 1)

#cs
  $Var = 123
  ConsoleWrite("$Var$") ; prints 123.
#ce

Local $Name = [A-Z]\.\s*[A-Z][a-z]+
Local $avMatch = StringRegExp($Line, "^(.+)\s($Name$)", 1)

If IsArray($avMatch) Then ConsoleWrite($avMatch[1]) ; the second index.

; or more naturally:
$avMatch = StringRegExp($Line, "^(.+)\s(" & $Name & ")", 1)
If IsArray($avMatch) Then ConsoleWrite($avMatch[1])

The number of array elements must match the number of capturing groups. If there was no match the return value is not an array, but if it does match the upper bound of the array must match the number of capturing groups, even if only one capturing group participated in the match. Thus:

Local $avMatch = StringRegExp("123", "(?x)( a )|( b ( c ) )|(123)", 1)
ConsoleWrite(UBound($avMatch) & @TAB & $avMatch[3] & @CRLF)

Strangely enough the opposite is not true:

Local $avMatch = StringRegExp("123", "(?x)(123)|( a )|( b (c ) )", 1)
ConsoleWrite(UBound($avMatch) & @CRLF)

Edit: lol ( c ) turned into © or incorporated.

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