Personal tools

Difference between revisions of "Regexp"

From MohidWiki

Jump to: navigation, search
(See also)
 
 
(3 intermediate revisions by the same user not shown)
Line 10: Line 10:
 
  /[A-Z]:[0-3]\.\S{1,8}/
 
  /[A-Z]:[0-3]\.\S{1,8}/
 
describes a pattern that will match, for example, the strings ''B:1.HelloSir'' or '''A:0.Good''. But it will not match ''a:2.darn'' because the first letter must be uppercase.
 
describes a pattern that will match, for example, the strings ''B:1.HelloSir'' or '''A:0.Good''. But it will not match ''a:2.darn'' because the first letter must be uppercase.
 +
 +
Bounding a whole word:
 +
/\bWord\b/
  
 
==See also==
 
==See also==
Line 16: Line 19:
 
*[[Sed]]
 
*[[Sed]]
 
*[[Awk]]
 
*[[Awk]]
 +
*[[Tr]]
 
*[[Python]]
 
*[[Python]]
 
*[[Ruby]]
 
*[[Ruby]]

Latest revision as of 21:54, 3 November 2010

Regexp stands for regular expressions. Regular expressions are what made languages like Awk and Perl notorious. Regular expressions are expressions equating a pattern string. To write a regular expression in Perl on must insert it between slashes (/regexp/)For example:

/[a-z]/ 

means all the alphabet lowercase letters.

/[0-4]/

stands for all the digits from 0 to 4 inclusive.

/\d{2,5,7}/

stands for all the sequences of 2, 5 or 7 digits.

The interesting thing is that you can compose regexps into complex patterns. For example the regexp

/[A-Z]:[0-3]\.\S{1,8}/

describes a pattern that will match, for example, the strings B:1.HelloSir or 'A:0.Good. But it will not match a:2.darn because the first letter must be uppercase.

Bounding a whole word:

/\bWord\b/

See also