Posix Basic Regular Expressions
Last Updated :
24 May, 2022
POSIX stands for Portable Operating System Interface. It defines a set of standard operating system interfaces based on the UNIX OS. These standards are specified by the IEEE (Institute of Electrical and Electronics Engineers) society which maintains the compatibility between different operating systems. POSIX systems are theorized in such a way that data or code can be transferred seamlessly between any two systems that are POSIX compliant
POSIX standards define application programming interfaces (API) at both the system and user levels and command-line shells and utility interfaces for software compatibility and/or portability with various distributions of the Unix Operating System and other operating systems as well. The IEEE also owns the trademark of POSIX. Developers are encouraged to use POSIX as it saves time for developers all around the world along with providing easier means for portability.
History of POSIX:
Earlier, computer programmers had to write different programs from ground zero for every single computer model. This was time-consuming and in most cases very difficult as each computer model was somehow different than the other and special care had to be taken to write a program keeping their hardware in mind. After AT&T Bell Labs launched the Unix OS, nothing was the same anymore. Unix quickly became popular and various different distributions like BSD, Xenix, etc. came into the picture. The introduction of newer operating systems like Unix would make it even more difficult for developers to maintain their software among so many devices and now operating systems too. This prompted the establishment of some standards that later came to be known as POSIX. They were first released under the name IEEE Standard 1003.1-1988 in the year 1988. The main aim was to establish some sort of pre-defined rules for future systems to maintain portability regardless of their hardware or manufacturers.
What are Regular Expressions?
Regular Expressions, often shortened as regex, are nothing but character sequences that provide search patterns in the text. They are used for various string-based operations like searching, finding & replacing, validating input, etc.
Regex finds its use in various search engines, text editors like MS Word, and even in text processing utilities such as AWK or sed. Most general-purpose programming languages like C, C++, Python, JS, Java, etc. support regex.
POSIX Regular Expressions:
One of the standards of POSIX defines two methods for using regular expressions. "grep" and "egrep" is used to implement regular expressions on POSIX systems.
In POSIX Basic Regex syntax, most chars are treated as literals, i.e. they match only themselves (e.g. j will match with "j"). However, there are some exceptions, which are called Metacharacters.
Metacharacters | Descriptions |
---|
. | To match any character once. The dot character matches a literal dot, within POSIX bracket expressions. For example, a.c matches "abc", etc., but [a.c] matches only "a", ".", or "c". |
- | Used to define a range. For Example, [a-d] will match for chars a to d, both inclusive. |
[ ] | To match anything inside the square brackets. For e.g. [ab] will match a or b. |
^ | The ^ (caret) within the square brackets negates the expressions. For example, [^a] will match anything except a. |
$ | The dollar symbol matches the ending position of the string if it is the last character of the regular expression. |
* | To match the preceding character 0 or more times. For example, a*d will match and, annnd, aaaaad, etc. |
{n} | To match the preceding chars n times. Example, [0-9]{3} will match 123, 234, 345, etc. |
{n,m} | To match preceding char at least n times and not more than m times. Example, [0-9]{3,5} will match 123, 3456, 45668, etc. |
Some examples:
1. To match any three-letter string ending with at. For example, cat, hat, etc.
.at
2. To match all strings ending in at except bat.
[^b]at
3. To match hat and cat, but only at the beginning of the string or line.
^[hc]at
4. To match uppercase letters.
[:upper:] (similar to [A-Z])
5. To match lowercase letters.
[:lower:] (similar to [a-z])
6. To match whitespace characters.
[ \t\n\r\f\v]
Similar Reads
Regular Expressions In R
Regular expressions (regex) are powerful tools used in programming languages like R for pattern matching within text data. They enable us to search for specific patterns, extract information, and manipulate strings efficiently. Here, we'll explore the fundamentals of regular expressions in R Program
5 min read
Kotlin Regular Expression
Regular expressions (regex) are powerful tools used for pattern matching and text manipulation. They are fundamental in almost every modern programming language, and Kotlin is no exception. In Kotlin, regular expression support is provided through the Regex class. An instance of this class represent
4 min read
Regular Expressions in Cucumber
Cucumber is a testing framework that supports Behavior-Driven Development (BDD). It enables us to write feature files using natural language like English, which makes them easy to read and understand. One of its features is writing step definitions in Cucumber using regular expressions. In this arti
6 min read
Escaped Periods in R Regular Expressions
Regular expressions (regex) are a powerful tool for pattern matching and text manipulation in R. Understanding how to use special characters, such as the period (.), is crucial for crafting accurate and effective regular expressions. In this article, we will focus on the role of escaped periods in R
3 min read
Properties of Regular Expressions
Regular expressions, often called regex or regexp, are a powerful tool used to search, match, and manipulate text. They are essentially patterns made up of characters and symbols that allow you to define a search pattern for text. In this article, we will see the basic properties of regular expressi
7 min read
Regular Expression to DFA
The main function of regular expressions is to define patterns for matching strings; automata theory provides a structured pattern recognition of these patterns through Finite Automata. A very common method to construct a Deterministic Finite Automaton (DFA) based on any given regular expression is
6 min read
Regular Expressions in Scala
Regular Expressions explain a common pattern utilized to match a series of input data so, it is helpful in Pattern Matching in numerous programming languages. In Scala Regular Expressions are generally termed as Scala Regex. Regex is a class which is imported from the package scala.util.matching.Reg
5 min read
MariaDB - Regular Expression
MariaDB is also a relational database language that is similar to SQL. However, the introduction of MariaDB took place as it is an extension to SQL and contains some more advanced operators rather than SQL. MariaDB contains operators similar to SQL like CRUD operations and between operators and othe
8 min read
Ruby | Regular Expressions
A regular expression is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings. Ruby regular expressions i.e. Ruby regex for short, helps us to find particular patterns inside a string. Two uses of ruby regex are Validation and Parsing. Ruby regex can
3 min read
Perl | Backtracking in Regular Expression
In Perl, a Regular expression(a.k.a regexes or regexps or REs) is a way of describing a set of strings without having to list all strings in your program or simply we can say that it is a sequence of characters that are used for pattern matching. In Perl, regular expressions have different uses: Fir
3 min read