Computing.Net > Forums > Programming > How to use a regular expression witihin batch

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to use a regular expression witihin batch

Reply to Message Icon

Name: albert_newton
Date: August 3, 2009 at 13:18:21 Pacific
OS: Windows Vista
Product: Samsung Quicklaser-6050 laserprinter 12ppm 33mhz pcl-6
Subcategory: Batch
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: August 3, 2009 at 16:58:45 Pacific
Reply:

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


0

Response Number 2
Name: Mechanix2Go
Date: August 3, 2009 at 22:58:24 Pacific
Reply:

@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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: How to use a regular expression witihin batch

How to replace a string using sed? www.computing.net/answers/programming/how-to-replace-a-string-using-sed/18003.html

how to create a batch 837 file www.computing.net/answers/programming/how-to-create-a-batch-837-file/15547.html

How to run a batch file on another www.computing.net/answers/programming/how-to-run-a-batch-file-on-another-/15653.html