REGEX: Matching Null?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting REGEX: Matching Null?
# 1  
Old 06-05-2008
[SOLVED]: REGEX: Matching Null?

I'm using the URL Regex feature of Squid for allowing sites via a list of regex strings to match allowed domains. The regex was actually copied from our previous proxy solution and it seemed to "just work". But, we've recently discovered that some domains (likely due to virtual hosts or host header configuration depending on if it's Apache or IIS respectively) fail if they are used without the www prefix in the URL. Below is an example of what sometimes works:

Code:
http://.*\.microsoft\.com/.*

The '.*\.' before the 'microsoft\.com' portion SHOULD mean, any number of any characters (zero or more) followed by a '.' I see the error in terms of the '\'. portion of the regex and plan to fix that. However, I've been unable to find a way to match both 'www.microsoft.com' and 'microsoft.com'. Here's what I thought would work:

Code:
http://[!.*|.*\.]microsoft\.com/.*

I admit to being really bad with regex, so please don't be too hard on me please. Smilie I've just never been able to "get it" 100%. Needless to say, the above doesn't work for me at all. It matches neither 'microsoft.com' nor 'www.microsoft.com'. I've tried some limited testing with 'grep' to try and find an adequate solution. But, what is it that I'm really trying to match? At first, I assumed I wanted a whitespace character, but I'm not looking for ' microsoft.com'. Then I thought, a null? But that seems to be impossible to match since it's not really a match at all since there's no character there. I'm sure someone who is an expert at regex would look at this and provide something insanely simple. I really don't want to do this:

Code:
http://[.*\.microsoft\.com/.*|microsoft\.com/.*]

or worse, this:

Code:
http://.*\.microsoft\.com/.*
http://microsoft\.com/.*

Any suggestions? Thanks in advance...

Last edited by deckard; 06-05-2008 at 11:57 AM.. Reason: Received a solution to the problem.
# 2  
Old 06-05-2008
I am unfamiliar with Squid, and maybe regexps work differently there, but it looks to me like you need the '?' operator which matches the preceding expression 0 or 1 times, e.g.
Code:
http://(www\.)?microsoft\.com/

does what you want when used as a grep argument.
# 3  
Old 06-05-2008
Thanks!

Your suggestion wound up working for me. I changed all of my lines to the following format:

Code:
http://(.*\.)?microsoft\.com/.*

That seems to have worked well. I knew someone on here would find this to be a simple problem to solve. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Regex matching column awk

Hi all, I want to extract rows with the pattern ALPHANUMERIC/ALPHANUMNERIC in the 2nd column. I dont wan rows with more than 1 slash or without any slash in 2nd column. a a/b b a/b/c c a/b//c d t/y e r f /f I came up with the regex grep '\/$' file a a/b b a/b/c d t/y (3 Replies)
Discussion started by: jianp83
3 Replies

2. UNIX for Dummies Questions & Answers

Regex matching with grep -l

I am trying to find patterns in files using grep -l -e. I specifically am searching for abc. I want any file that has abc in it, but not just the letters abc. I am searching for a pattern a followed by b followed by c. I have tried egrep -l and also I have tried the following: grep -el... (2 Replies)
Discussion started by: newbie2010
2 Replies

3. Shell Programming and Scripting

matching a regex using egrep not working

Hi, I'm trying to validate if a string matches a regular expression, but it is not working. Am I missing something? Do I need to scape any of the characters? if echo 'en-GB' | egrep '({1,8})(-{1,8})*' >/dev/null; then echo Valid value fi Thanks in advance (6 Replies)
Discussion started by: skrtxao
6 Replies

4. Shell Programming and Scripting

How to list Matching Directories OR NULL w/o error message?

I want to be able to get all the directories in a path into a variable array, BUT if there ARE NO directories I want the Variable to be NULL and not echo any error message! If there ARE directories, this will get the list of the directories whose name begins with the string "20":... (6 Replies)
Discussion started by: pgorbas
6 Replies

5. Shell Programming and Scripting

Perl: Regex, string matching

Hi, I've a logfile which i need to parse and get the logs depending upon the user input. here, i'm providing an option to enter the string which can be matched with the log entries. e.g. one of the logfile entry reads like this - $str = " mpgw(BLUESOAPFramework):... (6 Replies)
Discussion started by: butterfly20
6 Replies

6. Shell Programming and Scripting

Matching using Regex inside a file

I need scan through some files, then open the file one by one and scan inside the file using perl to see if it contain a start tag and end tag which the end tag is the mirror image of the start tag, the start tag and end tag only have 5 char. And inside the file there is "http://". It is just a... (5 Replies)
Discussion started by: blueblur
5 Replies

7. Shell Programming and Scripting

sed - print only matching regex

Hi folks, Lets say I have the following text file: name, lastname, 1234, name.lastname@test.com name1, lastname1, name2.lastname2@test.com, 2345 name, 3456, lastname, name3.lastname3@test.com 4567, name, lastname, name4.lastname4@test.com I now need the following output: 1234... (5 Replies)
Discussion started by: domi55
5 Replies

8. Shell Programming and Scripting

Perl regex help - matching parentheses

Let's say I'm trying to match potentially multiple sets of parentheses. Is there a way in a regular expression to force a match of closing parentheses specifically in the number of the opening parentheses? For example, if the string is "((foo bar))", I want to be able to say "match any number of... (7 Replies)
Discussion started by: cvp
7 Replies

9. Shell Programming and Scripting

null string matching in sed?

Hello, I would assume the expression ^$ should match a null string. Yet when I run: echo -n | sed 's/^$/nullstring/' I get no output. Can anyone tell me why? (6 Replies)
Discussion started by: Allasso
6 Replies

10. Shell Programming and Scripting

find -regex: matching multiple extensions

I want to use find to locate files with two different extensions, and run a grep on the results. The closest I have gotten is incredibly slow and ugly: for i in `ls -laR|egrep -e '(.js|.css)'`; do find . -name $i -print|xargs grep -H searchBg; done; This method makes my eyes bleed. Help! ;) ... (2 Replies)
Discussion started by: r0sc0
2 Replies
Login or Register to Ask a Question