Regex for checking if a string is strictly alphanumeric

Considering you want to check for ASCII Alphanumeric characters, Try this: “^[a-zA-Z0-9]*$”. Use this RegEx in String.matches(Regex), it will return true if the string is alphanumeric, else it will return false. public boolean isAlphaNumeric(String s){ String pattern= “^[a-zA-Z0-9]*$”; return s.matches(pattern); } If it will help, read this for more details about regex: http://www.vogella.com/articles/JavaRegularExpressions/article.html

Split alphanumeric string between leading digits and trailing letters

You can use preg_split using lookahead and lookbehind: print_r(preg_split(‘#(?<=\d)(?=[a-z])#i’, “0982asdlkj”)); prints Array ( [0] => 0982 [1] => asdlkj ) This only works if the letter part really only contains letters and no digits. Update: Just to clarify what is going on here: The regular expressions looks at every position and if a digit is … Read more

converting a number base 10 to base 62 (a-zA-Z0-9)

OLD: A quick and dirty solution can be to use a function like this: function toChars($number) { $res = base_convert($number, 10,26); $res = strtr($res,’0123456789′,’qrstuvxwyz’); return $res; } The base convert translate your number to a base where the digits are 0-9a-p then you get rid of the remaining digits with a quick char substitution. As … Read more

Regex for alphanumeric, but at least one letter

^\d*[a-zA-Z][a-zA-Z0-9]*$ Basically this means: Zero or more ASCII digits; One alphabetic ASCII character; Zero or more alphanumeric ASCII characters. Try a few tests and you’ll see this’ll pass any alphanumeric ASCII string where at least one non-numeric ASCII character is required. The key to this is the \d* at the front. Without it the regex … Read more

How to strip all non alphanumeric characters from a string in c++?

Write a function that takes a char and returns true if you want to remove that character or false if you want to keep it: bool my_predicate(char c); Then use the std::remove_if algorithm to remove the unwanted characters from the string: std::string s = “my data”; s.erase(std::remove_if(s.begin(), s.end(), my_predicate), s.end()); Depending on your requirements, you … Read more

How to determine if a String has non-alphanumeric characters?

Using Apache Commons Lang: !StringUtils.isAlphanumeric(String) Alternativly iterate over String’s characters and check with: !Character.isLetterOrDigit(char) You’ve still one problem left: Your example string “abcdefà” is alphanumeric, since à is a letter. But I think you want it to be considered non-alphanumeric, right?! So you may want to use regular expression instead: String s = “abcdefà”; Pattern … Read more

Generate a random alphanumeric string in Cocoa

Here’s a quick and dirty implementation. Hasn’t been tested. NSString *letters = @”abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789″; -(NSString *) randomStringWithLength: (int) len { NSMutableString *randomString = [NSMutableString stringWithCapacity: len]; for (int i=0; i<len; i++) { [randomString appendFormat: @”%C”, [letters characterAtIndex: arc4random_uniform([letters length])]]; } return randomString; }