Computing.Net > Forums > Programming > Regex / Pattern Matching Windows

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

Reply to Message Icon

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!




Sponsored Link
Ads by Google

Response Number 1
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.

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: Regex / Pattern Matching Windows

command line grep for windows www.computing.net/answers/programming/command-line-grep-for-windows/11678.html

Advanced Java Issue- txt read in www.computing.net/answers/programming/advanced-java-issue-txt-read-in/15884.html

Perl regex for IP address www.computing.net/answers/programming/perl-regex-for-ip-address/16540.html