Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looking for a RegEx example

Status
Not open for further replies.

markdmac

MIS
Dec 20, 2003
12,340
US
Hi Guys, much as I know of VBScript, I am totally lost when it comes to regular expressions. I am hoping someone can provide me a simple example for the task I need to accomplish.

I want to open a text file. Enumerate each of the lines in the file to search for 3 lines of text that will each begin with specific text. I need to save those three lines if they are found into variables for later use and need to replace the text with new text.

The 3 lines of text I am looking for will each start with:
user_pref("network.proxy.

And I will be setting the following as the end state:
user_pref("network.proxy.http", "localhost";
user_pref("network.proxy.http_port", 8080);
user_pref("network.proxy.type", 1);

I would be grateful for any examples that can do all of the above. Thank you.
 
I can give you a snippet of code that I have that uses regular expressions. Hopefully you can use it to somehow create the code that you're needing. Hope it helps
Code:
CURR_PATH = shell.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path")
Set searchforOrant = New RegExp
searchforOrant.IgnoreCase = True
searchforOrant.Global = False
searchforOrant.Pattern = "q:\\orant\\bin"

set matchCount = searchforOrant.Execute(CURR_PATH)

If matchCount.Count <> 1 Then
   New_PATH = "Q:\ORANT\BIN;" & CURR_PATH
   Shell.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path", New_PATH, "REG_EXPAND_SZ"
 
Thanks TheKidd, I'm afraid this is similar with other examples I have found but doesn't demo for me the part I need which are combining the loop of the lines of text with checking for the pattern match.


For the sake of making this "easier" if a line of text is found that starts with my patter match I want to save that information to a variable and append to that variable each time a line is found. From there it can even just delete the line of code as I have working code to write the data I need to the file if these lines don't already exist.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
What is your actual code and where are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Here is what I have now. I can do what I want with string replacement but I am thinking this is a good opportunity for me to learn about using regular expressions.
I am not sure if I am taking the right approach though since I need to search three different lines. Any recommendations on my approach are appreciated.

Code:
Dim objFSO,oTextStream
Set objFSO = CreateObject("scripting.filesystemobject")

DefaultProfileDir = "C:\Temp"


Set oTextStream = oFSO.OpenTextFile(DefaultProfileDir & "\prefs.js")
'make an array from the data file
FileArray = Split(oTextStream.ReadAll, vbNewLine)
'close the data file
oTextStream.Close


'user_pref("network.proxy.http", "localhost";
'user_pref("network.proxy.http_port", 8080);
'user_pref("network.proxy.type", 1);


For Each Line In FileArray
'Need to check for appropriate line of code with RegEx
LineCheck = GetFirstMatch("user_pref("&Chr(34)&"network.proxy.",Line)
	If LineCheck >= 1 Then
		'Save the line to a variable
		SaveSettings = SaveSettings & Line & vbCrLf
		'Now I need to replace the line
		 'Need to determine which of the 3 lines we have found set new line text

		'Not sure how to do the above replace 
		
		'Now write the new line to the report
		Report = Report & NewLine & vbCrLf
	Else 
		Report = Report & Line
	End If
Next

oTextStream.Write Report
oTextStream.Close


Function GetFirstMatch(PatternToMatch, StringToSearch)
	Dim regEx, CurrentMatch, CurrentMatches

	Set regEx = New RegExp
	regEx.Pattern = PatternToMatch
	regEx.IgnoreCase = True
	regEx.Global = True
	regEx.MultiLine = True
	Set CurrentMatches = regEx.Execute(StringToSearch)

	GetFirstMatch = ""
	If CurrentMatches.Count >= 1 Then
		Set CurrentMatch = CurrentMatches(0)
		If CurrentMatch.SubMatches.Count >= 1 Then
			GetFirstMatch = CurrentMatch.SubMatches(0)
		End If
	End If
	Set regEx = Nothing
End Function

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Hi Mark,

this pattern should do the trick:
([\r\n]+)(user_pref\(\"network[\w\W\s\S]+\;)


For future working with RegExes, I recommend this tool to you:

It's a perfect little tool for experimenting with RegExes and finding the right pattern for matching and replacing.

:)

Cheers,
Andy

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Thanks MakeItSo. I'll give that a try and investigate that link later tonight when I can work on this project again.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
On matching-each-line basis.
[tt] .pattern="^\s*(?!//)\s*(user_pref\([""']network\.proxy\.)([\s\S]+)$"
[/tt]
 
Thanks PHV. I'll enjoy reading the msdn article.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Geez I tried providing an answer without realizing it was you markdmac. I didn't check the name before I responded. You're way ahead of me in this vbscript world:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top