leuce Posted September 26, 2016 Posted September 26, 2016 (edited) Hello everyone Is it possible to define a variable using regular expressions (e.g. using back-references)? I mean, if I have this text:<p>Some text here</p> and I use this regex pattern: (<p>)(.+?)(</p>) along with this backreference pattern: $1$2$3 ... is there a way to add only "Some text here" (i.e. $2) to a variable $foo? Thanks Samuel Edited September 26, 2016 by leuce
genius257 Posted September 26, 2016 Posted September 26, 2016 something like this? $s = "<p>Some text here</p>" $p = "(<p>)(.+?)(</p>)" $a = StringRegExp($s, $p, 3) $foo = $a[1] MsgBox(0, "", $foo) To show your appreciation My highlighted topics: AutoIt Package Manager, AutoItObject Pure AutoIt, AutoIt extension for Visual Studio Code Github: AutoIt HTTP Server, AutoIt HTML Parser
pluto41 Posted September 26, 2016 Posted September 26, 2016 #include <array.au3> #include <MsgBoxConstants.au3> Local $sInput = 'Some Text Here' Local $sOutput = StringRegExpReplace($sInput, '(\w+)\s(\w+)\s(\w+)', '$3/$2/$1') Local $sMsg = StringFormat("Input:\t%s\n\nOutput:\t%s", $sInput, $sOutput) ; Result String MsgBox($MB_SYSTEMMODAL, "Result", $sMsg) Local $aNewArray = StringSplit ( $sOutput, "/" ) ; Result Array after StringSplit _ArrayDisplay ( $aNewArray, "New Array" ) Local $sFoo = $aNewArray [1] ; Result String assigning $aNewArray [element 1] to $sFoo Msgbox ($MB_SYSTEMMODAL, "Result", "$sFoo = " & $sFoo ) Perhaps using StringSplit ()
Malkey Posted September 26, 2016 Posted September 26, 2016 And some more. $s = "<p>Some text here</p>" $p = "(<p>)(.+?)(</p>)" $foo = StringRegExpReplace($s, $p, "$2") ; Replacing or keeping only what is wanted. MsgBox(0, "$foo", $foo) ;or $foo2 = StringRegExp($s, $p, 3)[1] MsgBox(0, "$foo2", $foo2) ;or $foo3 = StringRegExpReplace($s, "</?p>", "") ; Replacing everything that is not wanted with "" (nothing). MsgBox(0, "$foo3", $foo3)
leuce Posted September 26, 2016 Author Posted September 26, 2016 3 hours ago, genius257 said: something like this? $s = "<p>Some text here</p>" $p = "(<p>)(.+?)(</p>)" $a = StringRegExp($s, $p, 3) $foo = $a[1] MsgBox(0, "", $foo) Yes, exactly that, thanks.
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