What are regular expressions or regex? - CodeByAkram

What are regular expressions or regex?

What are regular expressions or regex in Java?

Regular Expression CodeByAkram

Regular expressions define a search pattern for strings in Java. The abbreviation for regular expression is called as regex. The search pattern, it can be anything from a simple character or a fixed string or a complex expression and it can contain special characters describing the pattern. The pattern defined by the regex may match one or several times or not at all for a given string.
Regular expressions can be used to search, edit and manipulate text.
The process of analyzing or modifying a text with a regex is called: The regular expression is applied to the text/string. The pattern defined by the regex is applied on the text from left to right. Once a source character has been used in a match, it cannot be reused. For example, the regex aba will match ababababa only two times (aba_aba__).

Regex examples
A simple example for a regular expression is a (literal) string. For example, the Hello World regex matches the "Hello World" string. . (dot) is another example for a regular expression. A dot matches any single character; it would match, for example, "a" or "1".
The following tables lists several regular expressions and describes which pattern they would match.

Table 1. Regex example
Regex
Matches
this is text
Matches exactly "this is text"
this\s+is\s+text
Matches the word "this" followed by one or more whitespace characters followed by the word "is" followed by one or more whitespace characters followed by the word "text".
^\d+(\.\d+)?
^ defines that the patter must start at beginning of a new line. \d+ matches one or several digits. The ? makes the statement in brackets optional. \. matches ".", parentheses are used for grouping. Matches for example "5", "1.5" and "2.21".



No comments:

Post a Comment