Examples:
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
Letters can be checked in Python String using the isalpha() method and numbers can be checked using the isdigit() method.
A regular expression regexp followed by ? matches a string of one or zero occurrences of strings that matches regexp. In this expression (and the ones to follow), char is a regular expression that stands for a single character—for example, a literal character or a period ( . ).
?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).
public class HasCharacters {
public static void main( String [] args ){
if( args[0].matches(".*[a-zA-Z]+.*")){
System.out.println( "Has characters ");
} else {
System.out.println("Ok");
}
}
}
Test
$java HasCharacters "1 name"
Has characters
$java HasCharacters "10,000"
Ok
$java HasCharacters "na123me"
Has characters
$java HasCharacters "na 123, 000"
Has characters
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With