Wins the Offer-53. Represents a numeric string (C ++ / Java)

topic:

Implement function used to determine whether a string represents a value (including integer and fractional). For example, the string "+100", "5e2", "- 123", "3.1416" and "-1E-16" shows the value. But "12e", "1a3.14", "1.2.3", "+ - 5" and "12e + 4.3" neither.

analysis:

Use regular expressions to do that easily.

. "" 1: match any single character other than "\ n", and is included to match "\ n" is an arbitrary character, use pattern such as "[\ s \ S]" and the like;

2. "^": Matches the beginning of the string does not match any character, to match the "^" character itself, use "\ ^";

3. "$": Matches position of the input end of the string, does not match any character, to match the "$" character itself, use "\ $";

4. "*": matches zero or more times or subexpression preceding character, "*" is equivalent to "{0}", such as "\ ^ * b" matches "b", "^ b" , "^^ b", ...;

The "+": matching one or more of the preceding character or subexpression, equivalent to "{1}", such as "a + b" match "ab", "aab", "aaab", ... ;

6. "?": ZeroOrOne preceding matching character or subexpression, is equivalent to "(0,1)", such as "a [cd]?" Matches "a", "ac", "ad "; when this character immediately any other qualifiers"? * "," + "," "," {n} "," {n,} "," {n, m} "after that, when a match pattern" non-greedy. " "Non-greedy" pattern matching to search for possible short string, and the default "greedy" pattern matching to search for possible long string. For example, the string "oooo", "o +?" Matches only a single "o", and "o +" match all "o";

7. "|": the two matching condition logical "or" (Or) operation, as a regular expression "(him | her)" Match "itbelongs to him" and "it belongs to her", but does not match " itbelongs to them ".;

8. "\": the next character is marked as a special character, text, or octal references reverse escape, such as, "n" matches the character "n", "\ n" newline matching sequence "\\" match "\", "\ (" matching "(";

9. "\ w": matching letters or numbers or underscores, any letters or numbers or underscores, i.e., A ~ Z, a ~ z, 0 ~ 9, _ any one of;

10. "\ W": not match any letters, numbers, the underscore character;

11. "\ s": either one blank character matches any whitespace, including spaces, tabs, page breaks, etc., and "[\ f \ n \ r \ t \ v]" equivalent;

12. "\ S": not match any whitespace characters, and "[^ \ f \ n \ r \ t \ v]" equivalent;

13. "\ d": matching digits, any number, any one of a 0 to 9, is equivalent to "[0-9]";

14. "\ D": matches any non-numeric characters, is equivalent to "[^ 0-9]";

15. "\ b": matches a word boundary, that is, the position between a word and a space, which is the location and the space between words, do not match any character, such as, "er \ b" match "never" in "er "but does not match the" verb "in" er ";

16. "\ B": a non-word boundary matching, "er \ B" matches "verb" is "er", but does not match "never" in "er";

17. "\ f": Match for a website page, is equivalent to "\ x0c" and "\ cL";

18. "\ n": matching a newline, equivalent to "\ x0a" and "\ cJ";

19. "\ r": a carriage return match, equivalent to "\ x0d" and "\ cM";

20. "\ t": matches a tab, it is equivalent to "\ x09" and "\ cI";

21. "\ v": matches a vertical tab, equivalent to "\ x0b" and "\ cK";

22. "\ cx": Match control character indication "x", such as, \ cM matching Control-M or carriage return, the value of "x" must be in the "AZ" or "az", if not, c is assumed that "c" character itself;

23. "{n}": "n" is a non-negative integer n times exactly matching, such as, "o {2}" and "Bob" in the "o" does not match, but the "food" in the two " o "match;

24. "{n,}": "n" is a non-negative integer, to match at least n times, such as, "o {2,}" mismatch "Bob" in "O", and match all "foooood." In " o "," o {1,} "is equivalent to" o + "," o {0,} "is equivalent to" o * ";

25. "{n, m}": "n" and "m" is a non-negative integer, where n <= m, match at least n times, at most m times, such as, "o {1,3}" matching "fooooood" the first three o, 'o {0,1}' is equivalent to 'o?', note that spaces can be inserted between the comma and the number; such as "ba {1,3}" matches "ba" or "baa" or "baaa";

26. "x | y": Match "x" or "y", such as, "z | food" matching "z" or "food"; "(z | f) ood" matching "zood" or "food";

27. "[xyz]": character set, match any one character included, such as, "[abc]" matches "plain" in the "a";

28. "[^ xyz]": reverse character set matches any character not included, matches any character except "xyz", such as, "[^ abc]" matches "plain" in "p";

29. "[az]": range of characters, any character matching in the specified range, such as, "[az]" matches "a" to any of the lowercase letters "z" range;

30. "[^ az]": reverse range characters, not within the specified range to match any character, such as, "[^ az]" not match any "a" to any character "z" range;

program:

C++

#include <regex>
class Solution {
public:
    bool isNumeric(char* str){
        string s2 = "[\\+,-]?[0-9]*([\\.][0-9]*)?([e,E][\\+,-]?[0-9]+)?";
        string s1 = str;
        return regex_match(s1, regex(s2));
    }
};

Java

public class Solution {
    public boolean isNumeric(char[] str) {
        String string = String.valueOf(str);
        return string.matches("[\\+,-]?[0-9]*([\\.][0-9]*)?([e,E][\\+,-]?[0-9]+)?");
    }
}

Guess you like

Origin www.cnblogs.com/silentteller/p/12096761.html