Jump to content
Sign in to follow this  
Tiger

Text between brackets

Recommended Posts

Tiger

I would like to get the text between the brackets. How can I do this

Text: "This is the text (and this the text between the brackets)"


My UDFs:- _RegEnumKey

Share this post


Link to post
Share on other sites
Monamo

I would like to get the text between the brackets. How can I do this

Text: "This is the text (and this the text between the brackets)"

I know this can be handled with (I believe) one line using StringRegExpReplace(), but I'm still crappy at regular expressions. Here's the long version (weaponx will likely pop in and show the smart version :) ):

$sText = "This is the text (and this the text between the brackets)"
If StringInStr($sText, "(") And StringInStr($sText, ")") Then
    $iLeft = StringInStr($sText, "(")
    $sOutput = StringTrimLeft($sText, $iLeft)
    $iRight = StringInStr($sOutput, ")")
    $sOutput = StringTrimRight($sOutput, (StringLen($sOutput) - $iRight) + 1)
Else
    $sOutput = $sText
EndIf
ConsoleWrite($sOutput & @CRLF)

Edit StringRegExpReplace() method

OK, here's my two-line regular expression version:

$sText = "This is the text (and this the text between the brackets)"
$sOutput = StringRegExpReplace($sText,".*\(","")
$sOutput = StringRegExpReplace($sOutput,"\).*","")
ConsoleWrite($sOutput & @CRLF)
Edited by Monamo

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Share this post


Link to post
Share on other sites
weaponx

#include <String.au3>

_StringBetween ( $sString, $sStart, $sEnd [, $vCase [, $iSRE ]] )

Share this post


Link to post
Share on other sites
Xenobiologist

Hi,

can also do it in one line with RegExp.

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites
Tiger

Thanks. I would like to do this with RegExp


My UDFs:- _RegEnumKey

Share this post


Link to post
Share on other sites
weaponx

Thanks. I would like to do this with RegExp

Well _StringBetween uses StringRegExp if you set the SRE parameter to 1 or true.

This is basically one line of code, you can see the code for _StringBetween() in C:\Program Files\AutoIt\Includes\String.au3

Here is the relevent portion:

If $vCase = Default Or $vCase = -1 Then
            $vCase = '(?i)'
        Else
            $vCase = ''
        EndIf
        Local $aArray = StringRegExp($sString, '(?s)' & $vCase & $sStart & '(.*?)' & $sEnd, 3)
        If IsArray($aArray) Then Return $aArray
        Return SetError(1, 0, 0)

Share this post


Link to post
Share on other sites
Tiger

Thanks. I would like to do this with RegExp only. No includes


My UDFs:- _RegEnumKey

Share this post


Link to post
Share on other sites
weaponx

Thanks. I would like to do this with RegExp only. No includes

You mentioned that. And then I posted the code. It can be condensed into one line.

Share this post


Link to post
Share on other sites
Tiger

You mentioned that. And then I posted the code. It can be condensed into one line.

OK. Cann you post the code with one line

My UDFs:- _RegEnumKey

Share this post


Link to post
Share on other sites
weaponx

OK. Cann you post the code with one line

No. Not until you show that you attempted it yourself.

Share this post


Link to post
Share on other sites
Tiger

OK. I have wrote this.

#include <Array.au3>
$text = "This is the text (and this the text between the brackets)"
Local $aArray = StringRegExp($text, '.*()*.', 2)
_ArrayDisplay($aArray)

But this show the text but not the text between the brackets


My UDFs:- _RegEnumKey

Share this post


Link to post
Share on other sites
weaponx

#include <array.au3>
$sString = "<tag> VALUE </tag>"

$sStart = ">"
$sEnd = "<"

Local $aArray = StringRegExp($sString, '(?s)(?i)' & $sStart & '(.*?)' & $sEnd, 3)
If IsArray($aArray) Then _ArrayDisplay($aArray)

-or-

#include <array.au3>
$sString = "<tag> VALUE </tag>"
Local $aArray = StringRegExp($sString, '(?s)(?i)>(.*?)<', 3)
If IsArray($aArray) Then _ArrayDisplay($aArray)

Share this post


Link to post
Share on other sites
Tiger

Thanks. But how can I do this with brackets


My UDFs:- _RegEnumKey

Share this post


Link to post
Share on other sites
weaponx

Thanks. But how can I do this with brackets

Brackets are a reserved character in the regular expression so they must be escaped.

#include <array.au3>
$sString = "[tag] VALUE [/tag]"

$sStart = "[tag]"
$sEnd = "[/tag]"

Local $aArray = StringRegExp($sString, '(?s)(?i)\Q' & $sStart & '\E(.*?)\Q' & $sEnd & '\E', 3)
If IsArray($aArray) Then _ArrayDisplay($aArray)

Share this post


Link to post
Share on other sites
Tiger

Thanks.

Brackets replace

and then stringregexp


My UDFs:- _RegEnumKey

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  

×