So here, I used the search() method to search for the pattern specified in the first argument. # dot(.) metacharacter to match any character Let’s write a basic pattern to verify that the DOT matches any character except the new line. Any character means letters uppercase or lowercase, digits 0 through 9, and symbols such as the dollar ($) sign or the pound (#) symbol, punctuation mark (!) such as the question mark (?) commas ( ,) or colons (:) as well as whitespaces. Inside the regular expression, a dot operators represents any character except the newline character, which is \n. For example, (abc) will match to substring 'abc' Matches whatever regular expression is inside the parentheses. Matches any single character not in brackets. For example, If you are searching for one of the special characters you can use a \ to escape them Use to escape special characters or signals a special sequence. For example, P1|P2, where P1 and P2 are two different regexes. For example, will match either a, or, b, or c character Matches any single character in brackets. Match 1 or more repetitions of the regex. Matches 0 or more repetitions of the regex.
Matches pattern only at the start of the string. Metacharacters also called as operators, sign, or symbols.įirst, let’s see the list of regex metacharacters we can use in Python and their meaning. For example, ^ (Caret) metacharacter used to match the regex pattern only at the start of the string. Characters or sign like |, +, or *, are special characters. In Python, Metacharacters are special characters that affect how the regular expressions around them are interpreted. What is Metacharacter in a Regular Expression?
This was the case when we wanted to match full string. SampleRegex := regexp.MustCompile("^abc$")īut doesn’t give a match for below as it only matches the full string abcd This will enable us to do a full-string match. If we only want to match the complete string then we need to anchor the string with the caret character at the start and the dollar character at the end. Therefore it gives matches for abcīasically, it will give a match for any input string which contains “abc” as a substring. The above program gives a match for all the strings which contain “abc” as a substring. Match = sampleRegex.Match(byte("1abc23")) This regex will match any string having abc as a substring. Let’s say you have below regex having three simple characters abc
Not clear? Let’s understand it with an example. It matches the end of the input stringĬaret and Dollar Metacharacters are anchor characters that are used to match the start and end of the string respectively. Dollar Character ‘$’ – It is also an anchor character that is used at the end of the regex to make sure that the given input string is matched with the regex at the end.It matches the beginning of the input string Caret Character ‘^’ – It is an anchor character that is used at the start of the regex to make sure that the given input string is matched with the regex from the start.Below is a brief introduction of the caret character and dollar character in golang. Metacharacters are characters that have special meaning in regex. Caret character ‘^’ and Dollar character ‘$’ are metacharacters that are used in regex in golang.