![]() ![]() If we use negation sign ( ^ ) in a character class then the class matches any character that isn’t listed. Also, if it is the first character listed in the range, it can’t possibly indicate a range, so it will not be meta character in this case. Note that a dash is a metacharacter only within a character class, otherwise it matches the normal dash character. Note that order in which ranges are given doesn’t matter. We can specify multiple ranges in single construct e.g. Other useful character ranges are and which match digits and English lowercase letters. Range of charactersĪ dash " - " indicates a range of characters. Similarly, will match all heading tags i.e. mith will allow the words smith and Smith both. seprte will match all the words “seperate” “separate” and “separete”.Īnother example can be to allow capitalization of a word’s first letter e.g. Character classes are useful in creating spell-checkers.įor example, while “e” matches just an e, and “a” matches just an a, the regular expression matches either. The regular-expression construct "", usually called a character class, lets us list the characters we want to allow at that point in the match. Similarly, “cat$” matches only if the ‘cat’ is at the end of the line. The caret and dollar are special in that they match a position in the line rather than any actual text characters themselves.įor example, the regular expression “cat” finds ‘cat’ anywhere in the string, but “^cat” matches only if the ‘cat’ is at the beginning of the line. The start and end are represented with '^' (caret) and '$' (dollar) signs. Let’s explore the commonly used metacharacters to understand them better. Matcher matcher = pattern.matcher("Generally, Alex and Brian share a great bonding.") Let’s see a quick Java example to use regex for reference. The words are combined with grammar according to a set of rules to create an expression that communicates an idea. We can think of literal text acting as the words and metacharacters as the grammar. Regex gain usefulness from advanced expressive powers that their metacharacters provide.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |