I need to write a script that prints out a statement stating whether an IP is valid or invalid.  IP addresses are made up of four bytes (each with a valid range of 0-255).  valid examples: 127.0.0.1 , 123.244.100.1 , etc.  invalid examples: 124,44,2,2  ,  127.0.2,4  , 355.23.24.43 , etc.  I'm guessing the easiest way would be to use regex? but i am having some trouble with that.  I also thought about using split(), but im not sure how I would handle any other special characters that aren't a "."   Any help or advice would be great, thanks
4416
What's the best way to validate that an IP entered by the user is valid?  It comes in as a string.
4461
This prints 83  System.out.println(0123)   However this prints 123  System.out.println(123)   Why does it work that way?
55833
I have some client-side validation against a text box, which only allows numbers up to two decimal places with no other input.  This script was a basis for entering numeric values only, however it needs to be adapted so it can take a decimal point followed by only up to two decimal places.  I've tried things such as /[^\d].\d{0,2}, but then the replacement call wouldn't work, and I've got no idea how to do it.  Code  &lt;script type="text/JavaScript"&gt;   function valid(f) {     if (!/^\d*$/.test(f.value)) {       f.value = f.value.replace(/[^\d]/g,"");       alert("Invalid number");     }   } &lt;/script&gt;     Note  I need to match an empty string. If an empty string is provided and the form is submitted, the value defaults back to zero.
45544
I have to take a list (loads) of IP addresses in this format:   134.27.128.0  111.245.48.0  109.21.244.0   and turn them into this format with a pipe in-between (IPs made up)  134.27.128.0 | 111.245.48.0 | 109.21.244.0 | 103.22.200.0/22   I think it is a find and replace command like sed but I can't get it to work.
71603
This is a bc output, e.g.:  Input:  echo "scale=10; BLA-BLA-HERE-NOT-IMPORTANT" | bc   Output:   .3708446283953709207058828124021300754352578903651372655882743141882\ 77124645102027246581819139527644919407424570060822470537797066353573\ 96635.8038454068 days   Two Questions:   can the output be rounded to something like "0.3708..."? can I remove the "\n"-s from the end?   I can't find any max width option in bc.
81981
In python   re.sub(r"(?&lt;=.)(?=(?:...)+$)", ",", stroke )    To split a number by triplets, e.g.:   echo 123456789 | python -c 'import sys;import re; print re.sub(r"(?&lt;=.)(?=(?:...)+$)", ",",  sys.stdin.read());'  123,456,789   How to do the same with bash/awk?
93031
I am trying to write a script where the user can search for a five digit zipcode using an input statement and a search cursor. The zipcodes in the file though are nine characters long (i.e. xxxxx-xxxx) so I'm trying to use the wildcard character after five digits in the where clause  but I can't figure out either where to put it or the syntax I'm supposed to use. Currently I can get the use input zipcode to print out but when I try putting the % wildcard in I get an error. Can anyone help me?  Here is my code so far:  import arcpy   #set workspace work=raw_input("work= ") arcpy.env.workspace=work zip5=input("zip5= ") strZip= str(zip5)  def searchHospitals(work,zip5):     fc= "Hospital.shp"     whereClause= '"ZIPCODE" LIKE'+"'%s'"% strZip     print whereClause
26280
What is an easy way in Python to format integers into strings representing thousands with K, and millions with M, and leaving just couple digits after comma?  I'd like to show 7436313 as 7.44M, and 2345 as 2,34K.  Is there some % string formatting operator available for that? Or that could be done only by actually dividing by 1000 in a loop and constructing result string step by step?
7845
I want to try to convert a string to a Guid, but I don't want to rely on catching exceptions (   for performance reasons - exceptions are expensive for usability reasons - the debugger pops up  for design reasons - the expected is not exceptional   In other words the code:  public static Boolean TryStrToGuid(String s, out Guid value) {     try     {         value = new Guid(s);         return true;     }     catch (FormatException)     {         value = Guid.Empty;         return false;     } }   is not suitable.  I would try using RegEx, but since the guid can be parenthesis wrapped, brace wrapped, none wrapped, makes it hard.   Additionally, I thought certain Guid values are invalid(?)    Update 1   had a good idea to catch only FormatException, rather than all. Changed the question's code sample to include suggestion.    Update 2  Why worry about thrown exceptions? Am I really expecting invalid GUIDs all that often?   The answer is yes. That is why I am using TryStrToGuid - I am expecting bad data.  Example 1 . I might be parsing folder names, checking to see if the text after the final . is a GUID.  c:\Program Files c:\Program Files.old c:\Users c:\Users.old c:\UserManager.{CE7F5AA5-6832-43FE-BAE1-80D14CD8F666} c:\Windows c:\Windows.old   Example 2 I might be running a heavily used web-server wants to check the validity of some posted back data. I don't want invalid data tying up resources 2-3 orders of magnitude higher than it needs to be.  Example 3 I might be parsing a search expression entered by a user.     If they enter GUID's I want to process them specially (such as specifically searching for that object, or highlight and format that specific search term in the response text.)    Update 3 - Performance benchmarks  Test converting 10,000 good Guids, and 10,000 bad Guids.  Catch FormatException:    10,000 good:     63,668 ticks    10,000 bad:   6,435,609 ticks  Regex Pre-Screen with try-catch:    10,000 good:    637,633 ticks    10,000 bad:     717,894 ticks  COM Interop CLSIDFromString    10,000 good:    126,120 ticks    10,000 bad:      23,134 ticks   p.s. I shouldn't have to justify a question.
835
Over the years I have slowly developed a  that validates MOST email addresses correctly, assuming they don't use an IP address as the server part.  I use it in several PHP programs, and it works most of the time.  However, from time to time I get contacted by someone that is having trouble with a site that uses it, and I end up having to make some adjustment (most recently I realized that I wasn't allowing 4-character TLDs).  What is the best regular expression you have or have seen for validating emails?  I've seen several solutions that use functions that use several shorter expressions, but I'd rather have one long complex expression in a simple function instead of several short expression in a more complex function.
59820
What is the most efficient way in C# 2.0 to check each character in a string and return true if they are all valid hexadecimal characters and false otherwise? Example void Test() {     OnlyHexInString(&quot;123ABC&quot;); // Returns true     OnlyHexInString(&quot;123def&quot;); // Returns true     OnlyHexInString(&quot;123g&quot;); // Returns false }  bool OnlyHexInString(string text) {     // Most efficient algorithm to check each digit in C# 2.0 goes here }
59657
Regex.IsMatch( "foo", "[\U00010000-\U0010FFFF]" )    Throws: System.ArgumentException: parsing "[-]" - [x-y] range in reverse order.  Looking at the hex values for \U00010000 and \U0010FFF I get: 0xd800 0xdc00 for the  first character and 0xdbff 0xdfff for the second.  So I guess I have really have one problem. Why are the Unicode characters formed with \U split into two chars in the string?
24951
I wrote a function in C that converts a string to an integer and returns the integer. When I call the function I also want it to let me know if the string is not a valid number. In the past I returned -1 when this error occurred, because I didn't need to convert strings to negative numbers. But now I want it to convert strings to negative numbers, so what is the best way to report the error?  In case I wasn't clear about this: I don't want this function to report the error to the user, I want it to report the error to the code that called the function. ("Report" might be the wrong word to use...)  Here's the code:  s32 intval(const char *string) {     bool negative = false;     u32 current_char = 0;      if (string[0] == '-') {         negative = true;         current_char = 1;     }      s32 num = 0;     while (string[current_char]) {         if (string[current_char] &lt; '0' || string[current_char] &gt; '9') {             // Return an error here.. but how?         }          num *= 10;         num += string[current_char] - '0';         current_char++;     }      if (negative) {         num = -num;     }      return num; }
66513
Is there a way to debug a regular expression in Python? And I'm not referring to the process of trying and trying till they work :)  Here is how regexes can be debugged in Perl:   use re 'debug';  my $str = "GET http://some-site.com HTTP/1.1"; if($str =~/get\s+(\S+)/i) {     print "MATCH:$1\n"; }   The code above produces the following output on my computer when ran:   Compiling REx "get\s+(\S+)" Final program:    1: EXACTF  (3)    3: PLUS (5)    4:   SPACE (0)    5: OPEN1 (7)    7:   PLUS (9)    8:     NSPACE (0)    9: CLOSE1 (11)   11: END (0) stclass EXACTF  minlen 5 Matching REx "get\s+(\S+)" against "GET http://some-site.com HTTP/1.1" Matching stclass EXACTF  against "GET http://some-site.com HTTP/1.1" (33 chars)    0           |  1:EXACTF (3)    3        |  3:PLUS(5)                                   SPACE can match 1 times out of 2147483647...    4       |  5:  OPEN1(7)    4       |  7:  PLUS(9)                                     NSPACE can match 20 times out of 2147483647...   24       |  9:    CLOSE1(11)   24       | 11:    END(0) Match successful! MATCH:http://some-site.com Freeing REx: "get\s+(\S+)"
53391
echo "1.1.1.1" | awk '/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/ {print $1}'   How can I filter IPv4 addresses in a script, and how can I filter IPv6 addresses.  I mean more precisely then this so the oneliner should only output VALID IPv4 addresses or another script to output only VALID IPv6 addr. In different languages? like: awk, perl?  ty!
18302
I have a list of Unicode codepoints, but I don't know of a "simple" way to convert these hex values into the actual characters they represent...    I've heard that zsh has echo -e '\u0965', but I use bash 4.1.  Is there something as simple as the zsh method, for bash?
104482
What is the best way to convert an Int value to the corresponding Char in Utf16, given that the Int is in the range of valid values?