Composingand Using Regular Expressions
Composingand Using Regular Expressions
DefineRegular Expressions and Explain Their Purpose
Inthe programming language, a regular expression is an arrangement ofcharacters that illustrates a search pattern. It describes how one ormore strings match when one is searching through the whole text. Itcomprises two compositions of regular characters such as normalletters and special characters referred to as metacharacters (López,Romero, & Sharma, 2014). Regular characters have ordinary literalmeaning while metacharacters have a special meaning. The concept iscredited to Stephen Cole Kleene, a mathematician who invented the useof the regular language (Microsoft, 2017).
Regularexpressions have various uses. They are used in examining a patternthat exists in a string. Through the concept, one can test whetherphone numbers and credit card numbers are present in particular text.Additionally, regular expressions are used to find and replacecharacters in a text (López, Romero, & Sharma, 2014). They areused to identify individual texts within a document with a view ofremoving it or replacing it with a different text. Regular charactersare used to extract a substring from an entire string afteridentifying a matching pattern. The concept is utilised by searchengines when generating search results. When users search for aparticular item, regular expressions help produce results by findinga matching patterns. ProvideAt Least Three (3) Examples Which Demonstrate the Way RegularExpressions Work
Regularexpressions can be used to match numeric ranges. However, sinceregular expressions deal with texts rather that numerals one has tobe more careful. Simply using the ranges of numbers does not generatea valid regex. For example using [0-266] has three elements. Therange of characters of between zero and two, the character 6, and thecharacter six recurring once more. Consequently, the character groupcan match characters 0, 1, 2 or six and is similar to . Using aregex [0-8]canmatch single digit numbers from zero to eight. [1-8][0-8] is used tomatch double digit numbers from ten to eighty-eight. One has to useword boundaries to precede and follow a valid regex expression forexample ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]). Theuse of parenthesis groups the alternatives together (López, Romero,& Sharma, 2014).
Regularexpressions can also trim whitespace at the beginning and the end ofa string. It is done by using a regex to find and substitute. One canfind ^ [  ]+ and substitute it by an empty text to remove thepreceding white space. A search of [ ]+$ and replacing it withnothing removes the white space at the end of a string (Windham,2014).
Regularexpressions are used to grab HTML tags. The use of<TAG[^>]*>(.*?)</TAG> is beneficial in correspondingthe opening and the concluding pair of a HTML tag. The question markis used to make the star lazy and thus making sure it breaks beforethe last concluding tag (Windham, 2014).
Examinethe Shortcomings of Regular Expressions and Describe At Least Two (2)Situations Where Using Them Might Be Inappropriate
Fromthe definition, regular expressions only recognize regularcharacters. Consequently, in some strings, the concept cannot beapplied. Some regular expressions have exponential complexity.Consequently, they may take a long time to test strings. When theregular expressions make use of the alternative (|)it may give rise to many complex alternatives and hence make itdifficult to generate results. In such a case, the solution is to tryand simplify the expressions to narrow it down into fewer options(Microsoft, 2017).
Usingregular expressions when testing for patterns in a source code andnatural language may be inappropriate. The two strings are hard torecognize using regular expressions and in some instances it iscompletely impossible to use regular expressions(López, Romero, & Sharma, 2014).
López,F., Romero, V., & Sharma, G. (2014). Masteringpython regular expressions.Birmingham, England: Packt Publishing Ltd.
Microsoft.(2017). Uses for Regular Expressions. Retrieved fromhttps://msdn.microsoft.com/en-us/library/101eysae(v=vs.100).aspx
Windham,K. (2014). Introductionto regular expressions in SAS.Cary, North Carolina: SAS Institute Inc.