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.
Regex / Pattern Matching Windows
Name: bigdog2112 Date: December 1, 2008 at 18:28:13 Pacific OS: WinXP CPU/Ram: Quad-core Product: HP
Comment:
Ugh, I did this a couple months ago, but lost my work, and now cannot remember how, nor find an example. I have an environment variable named FILE and it is set to C:\Program Files\export\myhome\movie.jpg I want to test that name against the string "myhome" and act on it if it's a match. So, the FILE, as set above would match, as would myhome1, 1myhome, etc... But if it is C:\Program Files\yourhome\movie.jpg, it would not match. This is a basic regex thing that is easy to do in Perl and others. And I remember that it was simple in Windows.....but I can't figure it out. How do I do this test? Thanks!
Name: klint Date: December 2, 2008 at 05:47:18 Pacific
Reply:
I don't think it's that simple. One way to do it is to see if the string is the same as a copy of the string without the substring (pattern):
if "%FILE:myhome=%" == "%FILE%" (echo Not matched.) else (echo Matched.)
If anyone has a better suggestion, I would be interested to hear it.
0
Response Number 2
Name: FishMonger Date: December 2, 2008 at 08:02:32 Pacific
Reply:
My suggestion would be to do this in Perl, or Python, or any other language that supports regex's.
0
Response Number 3
Name: Judago Date: December 2, 2008 at 12:16:46 Pacific
Reply:
I'm not so clear on exactly what you are trying to do, it almost seems like all you want to do is "if /i "%var1%"=="string" whatever...".
It may be possible to use findstr in some situations to have some basic regex.
@ECHO OFF echo %userprofile%|findstr /r /i c:.\Documents.and.Settings\.*>nul if not errorlevel 1 echo %userprofile% seem to be a valid userprofile pause
This does have a few problems, for one to use a string that has spaces in it you either need to search for all of the words by adding quotes or replace space with wildcards. Also note the wildcard after "c:" I could not get it to work without this.
0
Response Number 4
Name: Mechanix2Go Date: December 3, 2008 at 10:35:21 Pacific
Reply:
@echo off & setLocal EnableDelayedExpansion
echo %FILE% | find "myhome" > nul if errorlevel 1 ( echo no match ) else ( echo match )
===================================== If at first you don't succeed, you're about average.
Summary: I like to search a file for a pattern matching string. I am familiar with grep in unix. I looked at wingrep but not able to use it on the command line. Is there a command line grep available for windo...
Summary: Hello So I am having a few issues with a school project. First is with my ReadWithScanner. It does not read in the file giving me a NullPointerException error on the line <Scanner in = new>. I ha...
Summary: Why are you putting a character class within a character class? \d is the shortcut for the [0-9] character class. Your regex is incomplete and makes the false assumption that the first 3 octets will ...