Sign in to follow this  
Followers 0
leuce

Use regex to define variable?

5 posts in this topic

#1 ·  Posted (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 by leuce

Share this post


Link to post
Share on other sites



something like this?

$s = "<p>Some text here</p>"
$p = "(<p>)(.+?)(</p>)"

$a = StringRegExp($s, $p, 3)
$foo = $a[1]

MsgBox(0, "", $foo)

 

Share this post


Link to post
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 ()

Share this post


Link to post
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)

 

Share this post


Link to post
Share on other sites
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.

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  
Followers 0