Jump to content

RegExp - return values in parenthesis at end of string


Zedna
 Share

Recommended Posts

I have constants A and B and C, in fact they are not one char but whole word
and they can be included at the end of test text in parenthesis
there can be only one of them but also two of them or all three separated by coma in any order
(A) or (A,B ) or (C,A) or (A,B,C) or (A,C,B ) ...
I need to get text in these parenthesis
 

; in comment at end of each line is what I want to get
Test1('some text') ; ''
Test1('some text (something)') ; ''
Test1('some (something) text') ; '' --> ignore other () not at the end
Test1('some (something) (A) text') ; '' --> I want only at end of string
Test1('some (something) text (A)') ; 'A'
Test1('some text (A)') ; A
Test1('some text (B)') ; B
Test1('some text (A,B,C)') ; 'A,B,C'
Test1('some text (A,C,B)') ; 'A,C,B'
Test1('some text (A,C,X)') ; 'A,C' --> not X
Test1('some text (ABC)') ; '' --> missing ,

Func Test1($text)
    $regexp = '.*? \(([A|B|C])\)'
    $ret = StringRegExpReplace($text, $regexp, '$1')
    ConsoleWrite('regexp: ' & $regexp & ' text: ' & $text & ' --> ' & $ret & @CRLF)

    $regexp = '\z\(([A|B|C])\)'
    $ret = StringRegExpReplace($text, $regexp, '$1')
    ConsoleWrite('regexp: ' & $regexp & ' text: ' & $text & ' --> ' & $ret & @CRLF)

    $regexp = '\z\(([A|B|C]{1,3})\)'
    $ret = StringRegExpReplace($text, $regexp, '$1')
    ConsoleWrite('regexp: ' & $regexp & ' text: ' & $text & ' --> ' & $ret & @CRLF)

    $regexp = '\z\(([A|B|C|,]{1,3})\)'
    $ret = StringRegExpReplace($text, $regexp, '$1')
    ConsoleWrite('regexp: ' & $regexp & ' text: ' & $text & ' --> ' & $ret & @CRLF)

    $regexp = '.*?\(([A|B|C]{1,3})\)'
    $ret = StringRegExp($text, $regexp, 3)
    If Not @error Then
        $ret = $ret[0]
    Else
        $ret = ''
    EndIf
    ConsoleWrite('not replace: regexp: ' & $regexp & ' text: ' & $text & ' --> ' & $ret & @CRLF)

    ConsoleWrite(@CRLF)
EndFunc

I don't know how to use z (at end of string) and how to incorporate coma separator into my RegExp expression
these my RexExp expressions are not working even for simple one value

In function Test1() should be only one correct RexExp but I have there more because I want to show some of my attempts.

EDIT: fixed mistake in last RegExp

I hope that for RegExp gurus this will be very easy :-)

Edited by Zedna
Link to comment
Share on other sites

A and B and C, in fact they are not one char but whole word

So you can't use character class

Try this :)

#Include <Array.au3>

; in comment at end of each line is what I want to get
Test1('some text') ; ''
Test1('some text (something)') ; ''
Test1('some (something) text') ; '' --> ignore other () not at the end
Test1('some (something) (A) text') ; '' --> I want only at end of string
Test1('some (something) text (A)') ; 'A'
Test1('some text (A)') ; A
Test1('some text (B)') ; B
Test1('some text (A,B,C)') ; 'A,B,C'
Test1('some text (A,C,B)') ; 'A,C,B'
Test1('some text (A,C,X)') ; 'A,C' --> not X
Test1('some text (ABC)') ; '' --> missing ,

Func Test1($text)
   Local $ret
   $ret = StringRegExpReplace($text, '(?x).*(  \(.*?\)$  )', "$1")
   If @extended = 0 Then $ret = ""
   $aret = StringRegExp($ret, '(?x)  (?<=\(|,)(A|B|C)(?=,|\))  ', 3)
   $ret = _ArrayToString($aret, ",")
   If $ret = -1 Then $ret = ""
   Msgbox(0,"", "text: " & $text & @crlf & "result: " & $ret)
EndFunc
Link to comment
Share on other sites

subquestion about using z in StringRegExp() - at the end of string

 

I absolutely don't know where and how to place z in RegExp patern.

 

Here is simpler version of my previous example:

I just want to get text contained in parenthesis but only in case that parenthesis are at the end of string

Test1('some text') ; ''
Test1('some text (something)') ; 'something'
Test1('some (something) text') ; '' --> ignore other () not at the end ==> here my RegExp returns 'something' because lack of \z
Test1('some (something) (A) text') ; '' --> I want only at end of string
Test1('some (something) text (A)') ; 'A'
Test1('some text (A)') ; A
Test1('some text (B)') ; B
Test1('some text (A,B,C)') ; 'A,B,C'
Test1('some text (A,C,B)') ; 'A,C,B'
Test1('some text (A,C,X)') ; 'A,C,X'
Test1('some text (ABC)') ; 'ABC'

Func Test1($text)
    $regexp = '.*?\((.*?)\)'
    $ret = StringRegExp($text, $regexp, 3)
    If Not @error Then
        $j = UBound($ret) - 1
        $ret = $ret[$j]
    Else
        $ret = ''
    EndIf
    ConsoleWrite('regexp: ' & $regexp & ' text: ' & $text & ' --> ' & $ret & @CRLF)
EndFunc
Output:

regexp: .*?\((.*?)\) text: some text --> 
regexp: .*?\((.*?)\) text: some text (something) --> something
regexp: .*?\((.*?)\) text: some (something) text --> something
regexp: .*?\((.*?)\) text: some (something) (A) text --> A
regexp: .*?\((.*?)\) text: some (something) text (A) --> A
regexp: .*?\((.*?)\) text: some text (A) --> A
regexp: .*?\((.*?)\) text: some text (B) --> B
regexp: .*?\((.*?)\) text: some text (A,B,C) --> A,B,C
regexp: .*?\((.*?)\) text: some text (A,C,B) --> A,C,B
regexp: .*?\((.*?)\) text: some text (A,C,X) --> A,C,X
regexp: .*?\((.*?)\) text: some text (ABC) --> ABC
 

Please help me to put z correctly into my RegExp to get correct result also in third test text.

Thanks.

Edited by Zedna
Link to comment
Share on other sites

Didn't you look at my code ?

In the first regex used, you can replace $ by z and get nearly the same as yours

Example

#Include <Array.au3>

Global $words[] = [3, "A", "B", "C"]

Test1('some text', $words) ; ''
Test1('some text (something)', $words) ; ''
Test1('some (something) text', $words) ; '' --> ignore other () not at the end
Test1('some (something) (A) text', $words) ; '' --> I want only at end of string
Test1('some (something) text (A)', $words) ; 'A'
Test1('some text (A)', $words) ; A
Test1('some text (B)', $words) ; B
Test1('some text (A,B,C)', $words) ; 'A,B,C'
Test1('some text (A,C,B)', $words) ; 'A,C,B'
Test1('some text (A,C,X)', $words) ; 'A,C' --> not X
Test1('some text (ABC)', $words) ; '' --> missing ,

Func Test1($text, $wds)
   Local $ret, $pattern = '('
   For $i = 1 to $wds[0]-1
      $pattern &= $wds[$i] & '|'
   Next
   $pattern &= $wds[$wds[0]] & ')'

   $ret = StringRegExpReplace($text, '(?x).*(  \(.*?\)\z  )', "$1")
   If @extended = 0 Then $ret = ""
   $aret = StringRegExp($ret, '(?x)  (?<=\(|,)' & $pattern & '(?=,|\))  ', 3)
   $ret = _ArrayToString($aret, ",")
   If $ret = -1 Then $ret = ""
   Msgbox(0,"", "text: " & $text & @crlf & "result: " & $ret)
EndFunc

Edit

In your code it should be like this (w/o the first question mark)

$regexp = '.*\((.*?)\)\z'
Edited by mikell
Link to comment
Share on other sites

This one was not so difficult

The first regex grabs the final parenthesis, you already know it because it's the same as yours

The 2nd one

(?<=\(|,)(A|B|C)(?=,|\))

means :

(?<=(|,) preceded by an opening parenthesis or a comma

(A|B|C) alternation in a capturing group

(?=,|)) followed by a comma or closing parenthesis

Edited by mikell
Link to comment
Share on other sites

The Reg.Exp. patterns in this example use the "z", an end of string anchor.

Test1('some text') ; ''
Test1('some text (something)') ; 'something'
Test1('some (something) text') ; '' --> ignore other () not at the end ==> here my RegExp returns 'something' because lack of \z
Test1('some (something) (A) text') ; '' --> I want only at end of string
Test1('some (something) text (A)') ; 'A'
Test1('some text (A)') ; A
Test1('some text (B)') ; B
Test1('some text (A,B,C)') ; 'A,B,C'
Test1('some text (A,C,B)') ; 'A,C,B'
Test1('some text (A,C,X)') ; 'A,C,X'
Test1('some text (ABC)') ; 'ABC'

Func Test1($text)
    ;$regexp = '(?<=\()([^()]+)(?=\)\z)' ; Modified mikell's expression a look behind and look forward assertions also works.
    
    $regexp = '\(([^()]+)\)\z' ; \z, \Z, or $ could be used. They are all the same because all the test strings have no vertical white
    ; spaces (line feed characters).  Even if "(?m)" was present, the end of the string, "\z" or "\Z", would be the same as the end of line, "$".
    ; The \z, \Z, or $ anchors the close bracket and the correspondng open bracket to the end of the string.
    
    $ret = StringRegExp($text, $regexp, 3)
    If Not @error Then
        $ret = $ret[0] ; There can be only one element in the array, being the non-open bracket and non-close bracket characters within
        ;                parenthesis at the end of the string.
    Else
        $ret = ''
    EndIf
    ConsoleWrite('regexp: ' & $regexp & ' text: ' & $text & ' --> ' & $ret & @CRLF)
EndFunc   ;==>Test1

#cs Returns:-
regexp: \(([^()]+?)\)\z text: some text -->
regexp: \(([^()]+?)\)\z text: some text (something) --> something
regexp: \(([^()]+?)\)\z text: some (something) text -->
regexp: \(([^()]+?)\)\z text: some (something) (A) text -->
regexp: \(([^()]+?)\)\z text: some (something) text (A) --> A
regexp: \(([^()]+?)\)\z text: some text (A) --> A
regexp: \(([^()]+?)\)\z text: some text (B) --> B
regexp: \(([^()]+?)\)\z text: some text (A,B,C) --> A,B,C
regexp: \(([^()]+?)\)\z text: some text (A,C,B) --> A,C,B
regexp: \(([^()]+?)\)\z text: some text (A,C,X) --> A,C,X
regexp: \(([^()]+?)\)\z text: some text (ABC) --> ABC
#ce
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

×
×
  • Create New...