-1

I need to convert the following to Regex. I need the second, and third octets to be a wildcard. I am awful with Regex as I rarely ever need to use it. I have searched the web for a solution. Can someone help with this?

10.(Wildcard).(Wildcard).248

Thanks!

  • You should consider brushing up on regular expressions as wildcards are usually one of the first things you learn. Here's a [good tutorial](http://www.regular-expressions.info/tutorial.html) to get you started. – Mr. Llama Jan 07 '15 at 16:49
  • 2
    **Try writing something yourself** and then if it doesn't work, show us specifically what you did so we can help you along. **You start it, and then we help. We don't write it for you.** Show us the actual code that you've tried, and then describe what happened and what's not right, and then we can help you from there. Chances are you'll get pretty close to the answer if you just try it yourself first. – Andy Lester Jan 07 '15 at 16:50
  • Do you care whether the second and third octets are really octets (i.e. in the range 0-255), or just whether they're strings of digits? – Zero Piraeus Jan 07 '15 at 16:51

3 Answers3

4

Short and sweet:
10\.\d{1,3}\.\d{1,3}\.248 will get you pretty close, and is relatively simple.

  • Escape the dot with \. to prevent it from matching any character
  • Use \d to match any digit character
  • Use {1,3} to limit the number of consecutive digits to 1, 2, or 3

More complicated, but more exact:
To only match numbers between 0 and 255, you could replace \d{1,3} with ([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]):

10\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.248

- or -

10(\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])){2}\.248

Testing/developing regex patterns in the future
There are a lot of regex tester websites out there. I personally use RexexHero.net since I develop .Net applications, but there are other more generic options too such as regexpal.com.

tehDorf
  • 693
  • 1
  • 12
  • 30
0
10\.\d+\.\d+\.248

'\.' matches with '.', '\d' matches with any digit. '\d+' matches with 1 or more digits. Other characters matches with themselves.

Mark Shevchenko
  • 7,297
  • 1
  • 21
  • 25
  • Thank you for the quick response! All of these worked great for what I needed. – user4429725 Jan 07 '15 at 19:55
  • `10.99999.99999.248` could also match with this. Do you want the values to be in the range 0-255 like @ZeroPiraeus asked in a comment, or can any numbers be used? – OnlineCop Jan 07 '15 at 20:19
  • It depends on the source and format data. If you parse user's input, use difficult pattern, but if you parse logs, you can use simple pattern. It's clearer. – Mark Shevchenko Jan 07 '15 at 20:24
0

You'll need to escape the dot metacharacter with a backslash, but something like this will work:

10\.[0-9]{1,3}\.[0-9]{1,3}\.248

Note: this pattern isn't bulletproof. It doesn't check if the IP address is valid, it only checks if it matches an IP-like pattern. For example, it will match 10.999.999.248.

Mr. Llama
  • 18,561
  • 2
  • 47
  • 99
  • This would allow `10.999.999.248` if you don't limit the first digit to be limited to 0 or 1. – OnlineCop Jan 07 '15 at 20:20
  • @OnlineCop - Correct, but given OP's current knowledge of regular expressions, I didn't want to make the answer needlessly complicated. (e.g.: The first digit can only be 0, 1, 2 if it's three digits, the second digit can be 0-9 unless it's three digits and the first digit is a 2, etc) – Mr. Llama Jan 07 '15 at 20:50