Tiger Posted April 4, 2008 Share Posted April 4, 2008 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 Link to comment Share on other sites More sharing options...
Monamo Posted April 4, 2008 Share Posted April 4, 2008 (edited) 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 April 4, 2008 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 More sharing options...
weaponx Posted April 4, 2008 Share Posted April 4, 2008 #include <String.au3> _StringBetween ( $sString, $sStart, $sEnd [, $vCase [, $iSRE ]] ) Link to comment Share on other sites More sharing options...
Xenobiologist Posted April 4, 2008 Share Posted April 4, 2008 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 More sharing options...
Tiger Posted April 4, 2008 Author Share Posted April 4, 2008 Thanks. I would like to do this with RegExp My UDFs:- _RegEnumKey Link to comment Share on other sites More sharing options...
weaponx Posted April 4, 2008 Share Posted April 4, 2008 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 More sharing options...
Tiger Posted April 4, 2008 Author Share Posted April 4, 2008 Thanks. I would like to do this with RegExp only. No includes My UDFs:- _RegEnumKey Link to comment Share on other sites More sharing options...
weaponx Posted April 4, 2008 Share Posted April 4, 2008 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. Link to comment Share on other sites More sharing options...
Tiger Posted April 4, 2008 Author Share Posted April 4, 2008 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 Link to comment Share on other sites More sharing options...
weaponx Posted April 4, 2008 Share Posted April 4, 2008 OK. Cann you post the code with one lineNo. Not until you show that you attempted it yourself. Link to comment Share on other sites More sharing options...
Tiger Posted April 4, 2008 Author Share Posted April 4, 2008 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 Link to comment Share on other sites More sharing options...
weaponx Posted April 4, 2008 Share Posted April 4, 2008 #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 More sharing options...
Tiger Posted April 4, 2008 Author Share Posted April 4, 2008 Thanks. But how can I do this with brackets My UDFs:- _RegEnumKey Link to comment Share on other sites More sharing options...
weaponx Posted April 4, 2008 Share Posted April 4, 2008 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 More sharing options...
Tiger Posted April 4, 2008 Author Share Posted April 4, 2008 Thanks. Brackets replace and then stringregexp My UDFs:- _RegEnumKey Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now