Jump to content
Sign in to follow this  
Zedna

RegExp - return values in parenthesis at end of string

Recommended Posts

Zedna

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

Share this post


Link to post
Share on other sites
mikell

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

Share this post


Link to post
Share on other sites
Zedna

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

Share this post


Link to post
Share on other sites
mikell

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

Share this post


Link to post
Share on other sites
mikell

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

Share this post


Link to post
Share on other sites
Malkey

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

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
Sign in to follow this  

  • Similar Content

    • mLipok
      By mLipok
      In April 5, 2013 I ask @Lazycat 
      he answer:
      Then I change this tool a little.
      Now I back to this and make bigger changed.
      Here is new version.
      Update History: = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2018/11/07 v3.0 * Changed: AU3Check compilant - mLipok * Changed: almost all Variables renamed - mLipok * Added: "Delete RegExp Results" - mLipok * Added: support for dual monitor - mLipok * Added: "full screen mode" - mLipok = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2018/11/08 v3.1 * Added: colors for each Edit control - used GUICtrlSetBkColor() - mLipok * Added: FullScreen option (Checkbox + INI + Remarks in Tip) - mLipok * Added: _IsChecked() - mLipok * Changed: WinMove() - change size of window using: WindowWidth and WindowHeight - mLipok = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2018/11/13 v3.2 * Added: If $bFullScreen Then GUICtrlSetFont() - mLipok * Added: WM_COMMAND , $EN_CHANGE - prevent CPU overheat - mLipok = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2018/11/29 v3.3 * Changed: $_g_idCheckbox_Clear - also clear $_g_idEdit_Result - mLipok * Changed: ClearResult If GUICtrlRead($_g_idEdit_MatchText) = '' Or GUICtrlRead($_g_idEdit_MatchText) = '' - mLipok * Fixed: prevention CPU overheat - If $iGuiMsg <> 0 Then $_g_bWasAChange = True - any GUI change will fire RegExp result refresh - mLipok * Fixed: Top possition of $_g_idLabel_Dummy control - mLipok * Added: support for TabSwitch - CTRL+TAB and CTRL+SHIFT+TAB - mLipok = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  
    • MrCheese
      By MrCheese
      argh, pulling my hair out.
      considering this post: 
       
      say for a string = "03a", how can I strip out the leading 0 and the a.
      I have tried:
      $new = StringRegExpReplace($string, '[^1-9][^0-9]', '')
       
      and various combinations:
      ^0+[^0-9]
      [^[:digit:]]
      "[^0].*"
      "^0*(d+)"
       
      I'm going loopy!
       
       
    • PClough
      By PClough
      Hi everyone!
      After updating autoit, I tried to run an old program using complex regexp's.  It did not work.  Eventually I broke the problem down to this example:
       
      #include <Array.au3> $buf = "First title" & @CRLF & "Tom" & Chr(0x92) & "s sleepwalking" & @CRLF & "Last | line" & @CRLF $items = StringRegExp($buf, '([\x20-\xff]+)\x0d\x0a', 3) _ArrayDisplay($items,'') And this is the result I get when running it:
      Row 0
       
    • Miliardsto
      By Miliardsto
      Hello . How to do that
      $regexp = starts from "abcdef" and after this could be anything in name
      WinActivate($regexp)
×