473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

resolve environment variables in string - regular expression

Hello,

how can I resolve envionment variables in a string.
e.g.

strVar = /myVar
resolve in
str1 = /mytest02/$MYVAR/mytest02 --/mytest02//myVar/mytest02
(unix)
str2 =$MYVAR/mytest03 --/myVar/mytest03 (unix)
str3 =%MYVAR%/mytest03 --/myVar/mytest03 (windows)
I would not set the variables in this time.

I think I need a little regular expression code snippet, but I have
not work with regular expression before.

Thanks for your help, Kai.

Feb 9 '07 #1
2 5228
On 9 fév, 12:30, "Kai Rosenthal" <kairosent...@tiscali.dewrote:
Hello,

how can I resolve envionment variables in a string.
e.g.

strVar = /myVar
resolve in
nothing. This raises a SyntaxError. Python is *not* a shell script
language.
str1 = /mytest02/$MYVAR/mytest02 --/mytest02//myVar/mytest02
(unix)
str2 =$MYVAR/mytest03 --/myVar/mytest03 (unix)
str3 =%MYVAR%/mytest03 --/myVar/mytest03 (windows)
I would not set the variables in this time.

I think I need a little regular expression code snippet,
Nope.

my_var = "/myVar"
str1 = "/mytest02/%s/mytest02" % my_var
str2 = "%(my_var)s/mytest03" % {'my_var': my_var}
import os
str3=os.path.join(my_var, "mytest03")

hth

Feb 9 '07 #2
On Feb 9, 6:47 am, "bruno.desthuilli...@gmail.com"
<bruno.desthuilli...@gmail.comwrote:
On 9 fév, 12:30, "Kai Rosenthal" <kairosent...@tiscali.dewrote:
Hello,
how can I resolve envionment variables in a string.
e.g.
strVar = /myVar
resolve in

nothing. This raises a SyntaxError. Python is *not* a shell script
language.
str1 = /mytest02/$MYVAR/mytest02 --/mytest02//myVar/mytest02
(unix)
str2 =$MYVAR/mytest03 --/myVar/mytest03 (unix)
str3 =%MYVAR%/mytest03 --/myVar/mytest03 (windows)
I would not set the variables in this time.
I think I need a little regular expression code snippet,

Nope.

my_var = "/myVar"
str1 = "/mytest02/%s/mytest02" % my_var
str2 = "%(my_var)s/mytest03" % {'my_var': my_var}
import os
str3=os.path.join(my_var, "mytest03")

hth

Here's a pyparsing snippet to maybe do what you want.

-- Paul
from pyparsing import Word,alphas
import os

substitutionVar = Word("$",alphas+"_")
substitutionVar.setParseAction( lambda t: os.getenv(t[0][1:],t[0]) )

envsub = lambda s : substitutionVar.transformString(s)

test = "$TEMP/mytest03"
print envsub(test)

prints:
C:\DOCUME~1\Paul\LOCALS~1\Temp/mytest03

Feb 10 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1:...
6
by: Chris Lasher | last post by:
Hello, I would like to create a set of very similar regular expression. In my initial thought, I'd hoped to create a regular expression with a variable inside of it that I could simply pass a...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
7
by: Brian Mitchell | last post by:
Is there an easy way to pull a date/time stamp from a string? The DateTime stamp is located in different parts of each string and the DateTime stamp could be in different formats (mm/dd/yy or...
20
by: Opettaja | last post by:
I am new to c# and I am currently trying to make a program to retrieve Battlefield 2 game stats from the gamespy servers. I have got it so I can retrieve the data but I do not know how to cut up...
4
by: Richard Levasseur | last post by:
(Forewarning, most of these problems and solutions come from being the only developer in a 1 server department with no budget, few resources, unresponsive IT, and non-technical managers, so thats...
3
by: Max | last post by:
Hello everyone! I'm writing a simple xml parser with Javascript, but i have some problems matching markupdecl because of elementdecl markupdecl ::= elementdecl | AttlistDecl | EntityDecl |...
11
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.