Jump to content

How to extract variables from a string?


ur
 Share

Recommended Posts

I need a function where it will get a string as parameter which will of syntax in VBScript like below

a&"asdasd"&asd&"as&dsf&gdf"&fs

Here in the above string what ever in the quotes we shouldn't replace them as they are taken as normal strings.And if the ampersand(&) if outside the quotes then we need to replace them with plus(+).And the normal words which are outside the quotes we need to append them with $.

So the output will be as below.

$a+"asdasd"+$asd+"as&dsf&gdf"+$fs.

 

Can you please help me on this.I'm unable to differentiate with & in the quotes and outside the quotes.

Don't know how to check whether a string is in quotes or not.

Link to comment
Share on other sites

:)

$str = 'a&"asdasd"&asd&"as&dsf&gdf"&fs&th'

Msgbox(0,"", _format($str))

Func _format($str)
   Local $tmp = ""
   Local $res = StringRegExp($str, '(?|[^&"]+|"[^"]*")', 3)
   For $i = 0 to UBound($res)-1
      If StringLeft($res[$i], 1) <> '"' Then $res[$i] = "$" & $res[$i]
      $tmp &= $res[$i] & "+"
   Next
   Return StringTrimRight($tmp, 1)
EndFunc

 

Edited by mikell
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

×
×
  • Create New...