In computing, regular expressions, also referred to as regex or regexp, provide a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters. A regular expression is written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification.

The following examples illustrate a few specifications that could be expressed in a regular expression:

  • The sequence of characters "car" in any context, such as "car", "cartoon", or "bicarbonate"
  • The word "car" when it appears as an isolated word
  • The word "car" when preceded by the word "blue" or "red"
  • A dollar sign immediately followed by one or more digits, and then optionally a period and exactly two more digits (for example "$10", or "$245.99").

Regular expressions can be much more complex than these examples.

For more information, you can visit the following link [1]

If you want to test your own regular expression with a sample text, visit the following link [2]

Python Implementation 1

import re _regExpReq=r"(?P<nodeId>\S*) (?P<remoteHost>\S*) (?P<remoteLogname>\S*) (?P<remoteUser>\S*) \[(?P<time>.*)\] \"(?P<firstLineofRequest>\S* \S*) \S*\" (?P<finalStatus>\S*) (?P<responseSize>\S*) (?P<responseTime>\S*) \"(?P<referer>[^\"]*)\" \"(?P<userAgent>[^\"]*)\" \"(?P<xForwardedFor>[^\"]*)\".*" prog=re.compile(_regExpReq) for line in file: result=prog.match(line) if result!=None: #parse the service name (considering the GET ... special service name) if len('firstLineofRequest').split("="))>1:'firstLineofRequest').split("=")[0]+"="'firstLineofRequest').split("=")[1] else:'firstLineofRequest').split("=")[0] 

Still have questions? We can help. Submit a case to Technical Support.

Last Modified On: October 18, 2018