Jump to content

Use regex to define variable?


leuce
 Share

Recommended Posts

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

#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 ()

Link to comment
Share on other sites

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)

 

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