Solved how to use regular expression and findstr?

October 31, 2016 at 06:33:41
Specs: Windows 64, Core i7 2GHz/16GB
Hi all,

I have an input text file and using a Batch file I want to get the value of height parameter which is like the following in my input text:

Limits of parameter<height>[0]={x:x=200}-<g>
and in some cases my Parameter needs to be rounded, for example:

Limits of parameter<height>[0]={x:x=200.72}-<g>
which will be rounded to 201
how can I get this numbers using findstr/C and regular expressions and how can I round them?

Thanks for any help!
Elahe

message edited by elaheh


See More: how to use regular expression and findstr?

Reply ↓  Report •


#1
October 31, 2016 at 17:07:20
✔ Best Answer
This worked in my tests.
It basically searches for the <height> tag in the input file, and displays the line containing it, then extracts and rounds the value before putting it in a variable.

setlocal enabledelayedexpansion
for /f "tokens=1,2 delims={" %%A in ('findstr /i "<height>" inputfile.txt') do (
	for /f "tokens=2 delims==" %%C in ("%%B") do (
		for /f "tokens=1 delims=}" %%D in ("%%C") do (
			for /f "tokens=1,2 delims=." %%E in ("%%D") do (
				set int=%%E
				if not "%%F"=="" (
					set dec=%%F
					set dec=!dec:~0,1!
					if !dec! GEQ 5 (
						set /a int+=1
					)
				)
				echo !int!
			)
		)
	)
)

The int variable contains the value you're looking for

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)

message edited by RainBawZ


Reply ↓  Report •
Related Solutions


Ask Question