How to use a regular expression witihin batch

August 3, 2009 at 13:18:21
Specs: Windows Vista

I am trying to use regular expression to get all the characters from a line until a specific character occurs.

For example, I have the following line:

context-root1/*.jsv"/>

I want to be able to just extract the word context-root1

(in other words, I want to be able to extract all the characters before the /). I am trying to do this in a batch file. The code I have so far which is relevnt for this is as follows:

set finalvar=!myvar:~7,100!

echo !finalvar!

where the first line is looking at the string:

<Uri AffinityCookie="JSESSIONID" AffinityURLIdentifier="jsessionid" Name="/contextroot1/*"/>

and getting the string:
contextroot1/*"/>

I need to be able to make it just get the string contextroot1. So in other words get all the words before the first / occurs.

I know that this would be possible with regular expressions, but not sure how it can be done.

I know this should be easily done with regular expresions, but I do not have a lot of experience with them.


See More: How to use a regular expression witihin batch

Report •


#1
August 3, 2009 at 16:58:45

you can use vbscript
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
	strLine = objFile.ReadLine
	If InStr(strLine,"AffinityCookie") > 0 Then
		s = Split(strLine,"Name="&""""&"/")
		ind = InStr(s(1),"/")
		WScript.Echo Mid(s(1),1,ind-1)
	End If 
Loop
objFile.Close

on command line


C:\test>more file.txt
<Uri AffinityCookie="JSESSIONID" AffinityURLIdentifier="jsessionid" Name="/contextroot1/*"/>

C:\test>cscript /nologo test.vbs
contextroot1

GNU win32 packages | Gawk


Report •

#2
August 3, 2009 at 22:58:24

@echo off > newfile & setLocal EnableDelayedExpansion

set str=context-root1/*.jsv"/>

for /f "tokens=1 delims=/" %%a in ("!str!") do (
set newstr=%%a
)

echo !newstr!


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions


Ask Question