AdWords
4.7K members online now
4.7K members online now
Use AdWords conversion tracking and reporting to measure your results. Have a question about Google Analytics? Post it here, on the Google Analytics Community!
Guide Me
star_border
Reply

Regex for filtering IP addresses with subnet mask

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hello, 

I am wondering if someone could help me understand how to implement the proper expression for a series of IP address filters that I am trying to implement on a Filtered View of our GA (so that we are excluding these IPs). The IP addresses provided have subnet masks at the end, which seems to be making things more complicated. I also don't really know regex at all, so have really just been trial and error imputing the filter expressions at this point, with no luck. 

 

Right now, I've imputed one of the filters like so: ^142\.25\.81.0/25*$

 

Am I on the right track here?

 

Much thanks,

Emily

 

 

2 Expert replyverified_user
1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by Dave_Davis (Top Contributor)
November 2015

Re: Regex for filtering IP addresses with subnet mask

Top Contributor
# 11
Top Contributor
Hi Emily,

The regex for 142.25.81.0/25 would be:

^142\.25\.81\.([1-9]|[1-9][0-9]|1([0-1][0-9]|2[0-6]))$

Let me know if it works.

Arnold Majlath, Google Analytics Top Contributor
Was my response helpful? If yes, please accept it as solution.
Circle Me On Google+ | Visit me @ Redfly Digital

View solution in original post

Re: Regex for filtering IP addresses with subnet mask

Top Contributor
# 2
Top Contributor
Hi Emily
I think you're making this harder than it needs to be on yourself.

A subnet mask simply means a range of IP addresses. But they're fixed in each octet to 255/254

So,why not just try something like this : ^142\.25\.81*

His assumes that the subnet mask is 16 like in your example above. I'm on mobile now so can't test it myself but try it and if it doesn't work, I'll dig a little deeper with you on desktop.

Re: Regex for filtering IP addresses with subnet mask

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭
Well, that wouldn't be a first! Thank-you for this info. So, based on what you've said above, I can apply this to all my filters? So with this other one: 142.25.78.0/23 I can enter in the expression of: ^142\.25\.78* as well?

Thank-you again for your help, it's much appreciated.

Cheers,
Emily

Re: Regex for filtering IP addresses with subnet mask

Top Contributor
# 4
Top Contributor
Hi Emily
Apologies, if the subnet mask is 23,no,you can't use this I'm afraid. The only way I know how to calculate this is to do it out on paper!

If you don't mind waiting, I'll do that when I get in this evening. Alternatively, someone else might chime in sooner.

Re: Regex for filtering IP addresses with subnet mask

Top Contributor
# 5
Top Contributor
By the way, how many IP addresses and subnets are we talking about here?

Re: Regex for filtering IP addresses with subnet mask

Visitor ✭ ✭ ✭
# 6
Visitor ✭ ✭ ✭
Ah, I see. I was hoping there was an expression generator online somewhere. It seemed that GA had one at some point but no longer seems to exist. I have four subnet masks: 23, 24, 25 and 26.

Thank-you for the feedback!

Cheers,
Emily

Re: Regex for filtering IP addresses with subnet mask

Top Contributor
# 7
Top Contributor
Let me ping a colleague and see if he can help.

Re: Regex for filtering IP addresses with subnet mask

Visitor ✭ ✭ ✭
# 8
Visitor ✭ ✭ ✭
Ty!

Re: Regex for filtering IP addresses with subnet mask

Top Contributor
# 9
Top Contributor
Hi Emily
It looks like you may have to match with something a little more complicated than I thought. Here's a good discussion on the topic with examples http://blog.markhatton.co.uk/2011/03/15/regular-expressions-for-ip-addresses-cidr-ranges-and-hostnam...

Re: Regex for filtering IP addresses with subnet mask

Visitor ✭ ✭ ✭
# 10
Visitor ✭ ✭ ✭
Ah, such is my life. Thank-you for the link!