Home >Java >javaTutorial >Java uses regular expressions to implement time and date judgment operation analysis

Java uses regular expressions to implement time and date judgment operation analysis

黄舟
黄舟Original
2017-10-12 10:27:051895browse

This article mainly introduces the common judgment operations of time and date in Java based on regular expressions. It briefly explains the meaning of common metacharacters in regular expressions and analyzes the judgment of common date and time formats in Java based on regular expressions in the form of examples. For operational skills, friends in need can refer to

This article describes the common judgment operations of time and date based on Java regular expressions. Share it with everyone for your reference, the details are as follows:

Recently, I need to complete a business that determines the time and date type entered by the user to perform corresponding operations. Naturally, regular expressions are indispensable to realize this function. If you improve regular expressions, its usage is actually very simple. You only need to remember this form and run it flexibly.

#$ matches the end position of the input string. When the Multiline property of the RegExp object is set, $ also matches the position before "\n" or "\r" ##*##{n}n is a non-negative integer. For example, "o{2}" cannot match "Bob". "o" in "food", but can match two o's in "food". ##{n,}{n,m}?##(pattern)(?:pattern)(?=pattern)(?!pattern)(?<=pattern)(?Reverse negative pre-check is similar to forward negative pre-check, except in the opposite direction. For example, "(?##x|y Matches x or y. For example, "z|food" can match "z" or "food". Matches "zood" or "food".
Metacharacters Description
\ Marks the next character as a special character, Or a literal character, or a backreference, or an octal escape character. For example, "\n" matches the character "n". "\\n" matches a newline character. The sequence "\\" matches "\" and "\(" matches "(".
^ matches the beginning of the input string. If set The Multiline property of the RegExp object, ^ also matches the position after "\n" or "\r"
matches the preceding subexpression zero. times or multiple times. For example, zo* can match "z" and "zoo". * is equivalent to {0,}. Subexpression one or more times. For example, "zo+" can match "zo" but not "z". + is equivalent to {1,}. #? Matches the preceding subexpression zero or one time. For example, "do(es)?" can match "do" in "does" or "does". ? is equivalent to {0, 1}.
n is a non-negative integer that matches at least n. Times. For example, "o{2,}" cannot match "o" in "Bob", but it can match all o's in "foooood". "o{1,}" is equivalent to "o+". 0,}" is equivalent to "o*".
m and n are non-negative integers, where n<=m . Match at least n times and at most m times. For example, "o{1,3}" will match the first three o's in "fooooood". "o{0,1}" is equivalent to "o?". Note that there cannot be a space between the comma and the two numbers.
When this character is followed by any other limiter (*, +,?, {n}, {n,}, {n,m}), the matching mode is non-greedy. The non-greedy mode matches as little of the searched string as possible, while the default greedy mode matches as much as possible. The string to search for. For example, for the string "oooo", "o?" will match a single "o", while "o+" will match all "o" dots. Matches any single character except "\n". To match any character including "\n", use a pattern like "[\s\S]".
Matches pattern and obtains this match. The obtained match can be obtained from the generated Matches collection, using the SubMatches collection in VBScript and $0 in JScript. …$9 properties. To match parentheses characters, use "\(" or "\)".
Matches pattern but does not obtain the matching result, which means that this is a non-acquisition match and is not stored for later use. This is useful when combining parts of a pattern using the or character "(|)". For example, "industr(?:y|ies)" is a simpler expression than "industry|industries".
Forward positive pre-check, match the search string at the beginning of any string matching pattern. This is a non-fetch match, that is, the match does not need to be fetched for later use. For example, "Windows(?=95|98|NT|2000)" can match "Windows" in "Windows2000", but cannot match "Windows" in "Windows3.1". Prefetching does not consume characters, that is, after a match occurs, the search for the next match begins immediately after the last match, rather than starting after the character containing the prefetch.
Forward negative pre-check, match the search string at the beginning of any string that does not match pattern. This is a non-fetch match, that is, the match does not need to be fetched for later use. For example, "Windows(?!95|98|NT|2000)" can match "Windows" in "Windows3.1", but cannot match "Windows" in "Windows2000".
Reverse positive pre-check is similar to forward positive pre-check, but in the opposite direction. For example, "(?<=95|98|NT|2000)Windows" can match "Windows" in "2000Windows", but cannot match "Windows" in "3.1Windows".
[xyz] Character collection. Matches any one of the characters contained. For example, "[abc]" would match the "a" in "plain".
[^xyz] Negative value character set. Matches any character not included. For example, "[^abc]" would match "plin" in "plain".
[a-z] Character range. Matches any character within the specified range. For example, "[a-z]" matches any lowercase alphabetic character in the range "a" through "z".
Note: Only when the hyphen is inside the character group and appears between two characters, it can represent the range of characters; if it appears at the beginning of the character group, it can only represent the hyphen itself.
[^a-z] Negative character range. Matches any character not within the specified range. For example, "[^a-z]" matches any character that is not in the range "a" through "z".
\b Matches a word boundary, which refers to the position between a word and a space. For example, "er\b" matches the "er" in "never" but not the "er" in "verb".
\B Matches non-word boundaries. "er\B" can match the "er" in "verb", but not the "er" in "never".
\cx Matches the control character specified by x. For example, \cM matches a Control-M or carriage return character. The value of x must be one of A-Z or a-z. Otherwise, treat c as a literal "c" character.
\d Matches a numeric character. Equivalent to [0-9].
\D Matches a non-numeric character. Equivalent to [^0-9].
\f Matches a form feed character. Equivalent to \x0c and \cL.
\n Matches a newline character. Equivalent to \x0a and \cJ.
\r Matches a carriage return character. Equivalent to \x0d and \cM.
\s Matches any whitespace character, including spaces, tabs, form feeds, etc. Equivalent to [ \f\n\r\t\v].
\S Matches any non-whitespace character. Equivalent to [^ \f\n\r\t\v].
\t Matches a tab character. Equivalent to \x09 and \cI.
\v Matches a vertical tab character. Equivalent to \x0b and \cK.
\w Matches any word character including an underscore. Equivalent to "[A-Za-z0-9_]".
\W Matches any non-word character. Equivalent to "[^A-Za-z0-9_]".
\xn Matches n, where n is the hexadecimal escape value. The hexadecimal escape value must be exactly two digits long. For example, "\x41" matches "A". "\x041" is equivalent to "\x04&1". ASCII encoding can be used in regular expressions.
\num Matches num, where num is a positive integer. A reference to the match obtained. For example, "(.)\1" matches two consecutive identical characters.
\n Identifies an octal escape value or a backreference. If \n is preceded by at least n fetched subexpressions, n is a backward reference. Otherwise, if n is an octal number (0-7), then n is an octal escape value.
\nm Identifies an octal escape value or a backreference. If there are at least nm get subexpressions before \nm, nm is a backward reference. If \nm is preceded by at least n obtains, then n is a backward reference followed by the literal m. If none of the previous conditions are met, and if n and m are both octal numbers (0-7), then \nm will match the octal escape value nm.
\nml If n is an octal number (0-7), and m and l are both octal numbers (0-7), match the octal escape Value nml.
\un Matches n, where n is a Unicode character represented by four hexadecimal digits. For example, \u00A9 matches the copyright symbol (©).

那么利用上面的规则就可以完成日期时间类型的判断。下面的代码可以完成这样功能。


public static boolean isDateTime(String datetime){
  Pattern p = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1][0-9])|([2][0-4]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
  return p.matcher(datetime).matches();
}

通过上面的代码我们可以判断日期时间类型,而且它还可以判断纯日期类型,支持的格式为“YYYY-MM-DD HH:mm:ss”和“YYYY-MM-DD”。

当然如果是仅仅判断“YYYY-MM-DD”类型的话,就用下面的代码:


public static boolean isDate(String date){
  Pattern p = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))?$");
  return p.matcher(date).matches();
}

那么我们如果是要判断“HH:mm:ss”类型就需要用下面的代码来完成对这样的类型的判断。

代码如下:


public static boolean isTime(String time){
  Pattern p = Pattern.compile("((((0?[0-9])|([1][0-9])|([2][0-4]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
  return p.matcher(time).matches();
}

有了上面的三个代码,就可以完成对日期和时间的判断了。

The above is the detailed content of Java uses regular expressions to implement time and date judgment operation analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn