For example, //g and /(a|e|i|o|u)/g mean roughly the same thing. There is a special construct in regular expressions called character classes, they are placed within square brackets. What if we wanted to match all vowels? /(a|e|i|o|u)/g. We saw how we could match a 'w' or a 'c' using a group and a choice with (w|c). Thus, the previous example could have also been written as /g(oo)+gle.com/g Regex Character Classes They're simple shortcuts for commonly used ranges: Now we're ready to start talking about the special quantifier characters. We can capture any of these variations using a range quantifier: //g The following domains will all take you to Google:, ,. Google owns a number of variations on the domain '' with varying numbers of 'o's. Quantifiers come in two formats: ranges between curly braces and the special characters *, +, and ?. If we care how many times a regex factor occurs, meaning characters, groups, and classes (up next), we can use a quantifier. What else would this regex match that is not in this string? I'll leave this as an exercise to the reader. We can use a second group to match 'wood', too, with an or in the middle of the regex: /(w|c)(oul|oo)d/g We have essentially picked out the small part of the string we care about: the first letter of an 'ould' sounding word. Notice how there's a special match on the first letter. Let's look at a simple example: /(w|c)ould/g. They allow you to 'pick out' important parts of a large match with sub-expressions. Regex Matching GroupsĬreate matching groups by pairing parenthesis around a part of the regular expression. What if we wanted to choose between two options in the middle of a regex match string? For example, what if we wanted to match the words ending in an 'ood' or 'ould' sound in the classic tongue-twister 'How much wood would a woodchuck chuck'? We could repeat a lot of information or'ing each of the words like /wood|would|could/g or we could use matching groups and use a choice in the middle of the string. For example, /hifi|HiFi|high-fidelity/g, translates to 'hifi', or 'HiFi', or 'high-fidelity'. The vertical bar character | denotes the "OR" between options. This or That, Regex ChoicesĬhoices are the 'OR' of regular expressions. In order to match these three variations we can use regex choices, just like an OR. It will not match 'HiFi' or 'high-fidelity'. For example, the regular expression /hifi/g will match the string 'hifi'. If you've ever used 'Find' in a program like your web browser or text editor you've used a tool akin to the most basic regular expressions. Most modern language regex implementations use a syntax like Perl/JavaScript's but the details for executing match and replace are language specific. We'll be using JavaScript style syntax, which is closely related to its Perl regex inspiration. It is web-based and written in JavaScript so no download is necessary. Use our free regex tester to follow along with this tutorial. We'll avoid details for now and revisit them in follow-up tutorials. In this introductory regular expression tutorial we'll take a high-level tour of primary concepts. Regular expressions have many applications: input validation, data extraction, and advanced search and replace are a few good examples. They're a concise, domain-specific language for pattern matching text. Regular expressions are a handy tool for developers to use.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |