Skip to main content

Regular Expressions

A regular expression is notation for specifying a set of strings. e.g., the set of all valid email addresses or the set of all binary strings with  an even number of 1s.

There are five basic operations for creating regular expressions, as below,

OperationRegular ExpressionYesNo
Concatenationaabaabaabaabevery other string
Logical OR
(Alternation)
aa | baabaa
baab
every other string
Replication
(Kleene closure)
ab*aaa
aba
abba
ε
ab
ababa
Groupinga(a|b)aabaaaab
abaab
every other string
Wildcarda..aabba
abaa
aa
aaaaa
  • Concatenation: the simplest type of regular expression is formed by concatenating a bunch of symbols together, one after the other, like aabaab. This regular expression matches only the single stringaabaab. We can perform simple spell checking by using the concatenation operation. For example, we could form the regular expression niether and then for each word in a dictionary check whether that word matches the regular expression. Presumably no word in the dictionary would match, and we would conclude that niether is misspelled.
  • Logical OR: the logical OR operator enables us to choose from one of several possibilities. For example, the regular expression aa | baab matches exactly two strings aa and baab. Many spam filters (e.g., SpamAssassin) work by searching for a long list of common spamming terms. They might form a regular expression such as AMAZING | GUARANTEE | Viagra. The logical OR operator enables us to specify many strings with a single regular expression. For example, if our phone number is 734-8527, we might like to know whether it spells out any word on the phonepad (2 = abc, 3 = def, 4 = ghi, 5 = jkl, 6 = mno, 7 = prs, 8 = tuv, 9 = wxy). The following regular expression specifies all of the 3^7 possible combinations (p|r|s)(d|e|f)(g|h|i)(t|u|v)(j|k|l)(a|b|c)(p|r|s). It turns out that the only English word that matches is the word regular. (Replace this example with decoding an IM message that uses the "phone code.")
  • Replication: the replication operator enables us to specify infinitely many possibilities. For example, the regular expression ab*a matches aaabaabbaabbba, an so forth. Note that 0 replications of b are permitted.
  • Grouping: the grouping operator enables us to specify precedence to the various operators. The replication operator has the highest precedence, then concatenation, then logical OR. If we want to specify the set of strings aabaababaabababa, and so forth, we must write (ab)*a to indicate that the ab pattern must be replicated together.
  • Wildcard: the wildcard symbol matches exactly one occurrence of any single character.


The first four basic operations above (concatenation, logical or, replication, grouping) are the theoretical minimum needed to describe regular expressions. Most programming environments support additional operations for convenience (including the wildcard operation), and Java is no exception. The table below includes some of the highlights.

OperationJava Regular ExpressionYesNo
One or morea(bc)+deabcde
abcbcde
ade
abc
Once or not at alla(bc)?deade
abcde
abc
abcbcde
Character classes[a-m]*blackmail
imbecile
above
below
Negation of character classes[^aeiou]b
c
a
e
Exactly N times[^aeiou]{6}rhythm
syzygy
rhythms
allowed
Between M and N times[a-z]{4,6}spider
tiger
jellyfish
cow
Whitespace characters[a-z\s]*hellohello
say hello
Othello
2hello


Comments

Popular posts from this blog

Stretch a row if data overflows in jasper reports

It is very common that some columns of the report need to stretch to show all the content in that column. But  if you just specify the property " stretch with overflow' to that column(we called text field in jasper report world) , it will just stretch that column and won't change other columns, so the row could be ridiculous. Haven't find the solution from internet yet. So I just review the properties in iReport one by one and find two useful properties(the bold highlighted in example below) which resolve the problems.   example:
<band height="20" splitType="Stretch"> <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true"> <reportElement stretchType="RelativeToTallestObject" mode="Opaque" x="192" y="0" width="183" height="20"/> <box leftPadding="2"> <pen lineWidth="0.25"/> …

JasperReports - Configuration Reference

Spring - Operations with jdbcTemplate

This class manages all the database communication and exception handling using a java.sql.Connection that is obtained from the provided DataSource. JdbcTemplate is a stateless and threadsafe class and you can safely instantiate a single instance to be used for each DAO.


Use of Callback Methods
JdbcTemplate is based on a template style of programming common to many other parts of Spring. Some method calls are handled entirely by the JdbcTemplate, while others require the calling class to provide callback methods that contain the implementation for parts of the JDBC workflow. This is another form of Inversion of Control. Your application code hands over the responsibility of managing the database access to the template class. The template class in turn calls back to your application code when it needs some detail processing filled in. These callback methods are allowed to throw a java.sql.SQLException, since the framework will be able to catch this exception and use its built-in excepti…