Jump to content

Text between brackets


Recommended Posts

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]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)
Link to comment
Share on other sites

#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)
Link to comment
Share on other sites

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)
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...