1

I have a text file and it is formated like this;

(1992){0103644}[00004]
(1999){0137523}[00019]
(1991){0103064}[00098]
(2001){0246578}[00103]
(2011){1399103}[00150]

What i am trying to do finding a variable (which is entered by user) inside this text. Variable had to be search between {} This is what i am trying;

setlocal EnableDelayedExpansion
set "SIRA=1"
set "TEST=103"
for /f "tokens=1-3 delims=(){}[]" %%a in ('findstr /irc:"\{[0-9]*!TEST![0-9]*\}" %MURL%') do (
  set FISIM[!SIRA!]=%%a
  set FYIL[!SIRA!]=%%b
  set FIDD[!SIRA!]=%%c
  set /a SIRA+=1
)
set /a SIRA-=1
for /l %%i in (1,1,!SIRA!) do call :_Yazdir "%%i" "!FISIM[%%i]!" "!FYIL[%%i]!" "!FIDD[%%i]!" "!SATIRRENGI!"
goto :EOF

it works ONLY if i write the variable inside the findstr (in this case its !TEST! replaced by 103). So is there a possible way to find it ?

PS: %MURL% contains the name of text file.

PS: :_Yazdir is the function that i use for print

2 Answers 2

1

I found why my code doesnt work. I was using a variable name which is like XXX[1]. So when findstr started regex search it was assuming [1] had to be on the text file.. Silly me i wasted hours to see this mistake :(

Sign up to request clarification or add additional context in comments.

1 Comment

You might like to consider deleting your question if you have the rights to do so, as your code was working.
0

If you can tap into a regex engine that supports lookarounds, you can use this regex:

(?<={)[^}]+(?=})

See demo

For instance, with grep in Perl mode, you can do:

grep -P "(?<={)[^}]+(?=})" myfile

In the comments you say you are on Windows, so you can use pcregrep (which is part of the PCRE distribution).

Explanation

  • The (?<={) lookbehind asserts that what precedes is a {
  • [^}]+ matches one or more chars that are not a }
  • The (?=}) lookahead asserts that what follows is a }

Reference

4 Comments

well i am using win8 and i cant install anything :(
On Win8 you have pcregrep, which you don't need to install. You can get a binary from this page. There may be other tools, I'm not too familiar with the shell, but pcregrep works.
Your way is prety good. But i have to try with limited regex from findstr :(. Using tools which is not directly came from win8 forbidden.
In PowerShell you can do $string -match and use the regex I gave you, i.e. something like $string -match '(?<={)[^}]+(?=})' Basically this is the right regex, your job is to find the tool that will work with it in your environment. If powershess doesn't work google something like Windows batch lookahead :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.