The goal of this regex is to pick up numbers inside an email address. However, we use in the picking of the username and the domain name, we don't want to directly match numbers yet. The first sub-match is for the email address. This function takes in a string and returns a list of strings that are the leftmost matches in the source string.įirst, we need to understand the regex to get a clear idea of the code snippet. We find the sub-matches in the string which will be handled by the FindStringSubmatch. The regex is a bit different from the previous example for understanding the sub-matches within an expression. We have a regex for matching either a mail address, the word mail or email, or a number up to 3 digits. In the above example, there are a few things to take away, let us break it down into small pieces. There is also a version of FindAll as FindAllString which will take in a string as the text source and return a slice of strings. It doesn't match the number 123 which is not a valid match for the given regular expression. Finally, we get the result as 12345-124 which are present in the file. We search for the pin code in the file and the funciton returns a list of bytes that match the regular expression in the provided object exp. If we use a negative number it will include all the matches. The FindAll method takes in the parameter as a slice of byte and the integer as the number of occurrences to search. We read the file pincode.txt as a slice of bytes and use the FindAll method. In this example, the exp is a regular expression for a postal code, which can either have 5 digit or 5digits-4digit combination. We have again read the contents from the file. In the above example, we have used the Compile method to create a regular expression and FindAll to get all the occurrences of the matching patterns in the text. One of the easy-to-understand ones include the MtachString, and Match methods.Įnter fullscreen mode Exit fullscreen mode In golang, the regexp package provides a few functions to simply match expressions with strings or text. The basic operations with regex or regular expression can be performed to compare and match if the pattern matches a given string. The regexp package provides some methods like Match, MatchString methods on a slice of bytes and string respectively from the pattern string. One of the fundamental aspects of the regular expression is to check if a particular pattern is present or not in a source string. This package also supports two types of methods serving different purposes and use cases for string and slice of bytes, this can be useful for reading from a buffer, file, etc., and also flexible enough to search for a simple string. It provides a Regexp type and a lot of methods on top of it to perform matching, finding, replacing, and sub-matches in the source text. We will be using the regexp package in the golang standard library to get some quick and important methods for quick and neat pattern matching and searching. Some resources to learn the fundamentals of regular expressions: However, it won't harm you if you are curious about it and want to explore it further. These relate to the study of the theory of computation quite closely but you don't need to get into too much theory in order to understand the working of regular expressions. So regular expressions use some pattern-matching techniques using basic logic operators like concatenation, quantifiers, etc. Might not be accurate, but makes sense to me after I had played and explored it (not fully). This is not the formal definition, but have written it in words for my understanding of regular expressions till now. Regular expressions are basic building blocks for searching, pattern matching, and manipulation from a source of text using computational logic. So, let's start with what are regular expressions. This will have examples for each of the concepts and similar variants will follow the same ideology in self-exploring the syntax. This article will cover the basic operations like matching, finding, replacing, and sub-matches in a regular expression pattern from string source or file content. In this 26th part of the series, we will be covering the basics of using regular expressions in golang.
0 Comments
Leave a Reply. |