Jump to content
Sign in to follow this  
Zedna

RegExp - return values in parenthesis at end of string

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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Tosyk
      Hi,
      Please help me to change metasymbol line. Right now I have this condition code:
      If StringInStr($_sName, 'TEXT ') Then $_sName = StringRegExpReplace($_sName, '(^.*)\TEXT (.*)$', '$2') $_sName = StringRegExpReplace($_sName, '(^.*)\ (.*)$', '$1') If Not CheckIsSave_($_sName) Then It work fine with this text file and finds each line which start from 'TEXT':
      Material B7E671143D244B ==================================== TEXT 2F3139D816C34D 1 TEXT B6A968EF2505A2 1 TEXT 35206697A04F91 1 TEXT EB485AF490D83D 1 TEXT 0DAB42294BD9B3 1 TEXT 3D6525BEE360E1 0 Material D6906B886B06E3 ==================================== TEXT 0CCECCCCFB62AE 1 TEXT 1E14CB29AB43F0 1 TEXT FB7F0DCE9B5950 1 But I have a new text file now the lines of which now are start with 0:, 1: and so on:
      sm_0 --------------- 0: dummy_gray 1: c_com_socksa_mt 2: c_com_socksa_tn 3: dummy_white 4: default_z 5: dummy_nmap 6: --- 7: --- sm_1 --------------- 0: c_com_prisoner_shoes_di 1: c_com_prisoner_shoes_mt 2: c_com_prisoner_shoes_tn 3: dummy_white 4: default_z 5: c_com_leatherb_rt 6: --- 7: --- how to change (or add) the condition code above to work with new text file?
      I'm trying to change this script: http://autoit-script.ru/threads/poisk-fajlov-rekursivno-po-dannomu-spisku.26970/post-148646
       
    • By seadoggie01
      I'm trying to capture everything after a "#ToDo" in my scripts. I got that like this:
      (?i)[^\v]*#todo(.*) But then I thought it would be nice to use underscores to continue the ToDo... kind of like this:
      #ToDo: This is a really long explanation about something _ # that is very in-depth and needs to take up a lot of _ # space in a ToDo comment Global $variables = "Bad" I can't seem to capture everything... and maybe I'm trying to do too much with Regex... I keep trying variations of this:
      Condensed Version: (?im)[^\v]*#todo(?:([^\v]*)_\s*)*#([^\v]*) Expanded with comments (?ixm)(?# Ignore case, ignore newlines in Regex, use multiline option)# [^\v]*(?# Match leading space/s)# \#todo(?# Match the #todo)# (?:([^\v]*)_\s*)*(?# Match lines ending with _)# \#([^\v]*)(?# Last line only, no _'s)# I never seem to be able to build an array well with Regex... I saw something once about not being able to capture repeated patterns, and I think that's my issue
    • By genius257
      Inspired by PHP's preg_split.
      Split string by a regular expression.
      Also supports the same flags as the PHP equivalent.
      v1.0.1
       
      Example:
      #include "StringRegExpSplit.au3" StringRegExpSplit('splitCamelCaseWords', '(?<=\w)(?=[A-Z])') ; ['split', 'Camel', 'Case', 'Words']  
    • By RAMzor
      Hi guys I need your help.
      I have string like this : "TDM111A5,      RCT222Y5/ 7  ; FDT444E4 /8 , ABC222R5"
      I need find a coma or semicolon and delete white spaces before and after them
      The output should be a string and/or array 
      String : "TDM111A5,RCT222Y5/ 7;FDT444E4 /8,ABC222R5"
      Array:
      TDM111A5
      RCT222Y5/ 7
      FDT444E4 /8
      ABC222R5
    • By BlueBandana
      Is there a way to output the regex matches into a file?
      I have a script to compare two files and check for regex matches.
      I want to output the matching regex of 'testexample.txt' to another file.
      #include <MsgBoxConstants.au3> #include <Array.au3> $Read = FileReadToArray("C:\Users\admin\Documents\testexample.txt") $Dictionary = FileReadToArray("C:\Users\admin\Documents\example.txt") For $p = 0 To UBound($Dictionary) - 1 Step 1 $pattern = $Dictionary[$p] For $i = 0 To UBound($Read) - 1 Step 1 $regex = $Read[$i] If StringRegExp($regex, $pattern, 0) Then MsgBox(0, "ResultsPass", "The string is in the file, highlighted strings: " ) Else MsgBox(0, "ResultsFail", "The string isn't in the file.") EndIf Next Next  
×
×
  • Create New...