SLS

 
Title: Howto: Regular Expressions basics
Description: A few tips for getting started with regular expressions.


Regular expressions overview


Several *nix tools are able to utilize regular expressions. For the examples in this howto we will be using grep.

  • Note that it is a good standard practice to use single quotes around your regular expressions to ensure they are interpreted by the shell correctly. The exception to this is when you want variable substitution to occur - then you should use double quotes of course.



Regular expressions and wildcards


Wildcard characters allow you to specify different possibilities for a single character position.

Wildcards include:
  • [abcd] : matches one of a, b, c or d.
  • [a-z] : matches one of any letters a through z.
  • [^abcd] : matches any character that is not a, b, c or d.
  • [^a-z] : matches any character that is not any of the letters a through z.
  • . : matches any single character except newline.
  • Note that uppercase can be used here as well, as in [A-Z] or [ABCD].


Examples with grep:

The command
grep 'Ti[nm]a Jones' staff-listing
would search the staff-listing file for any occurrences of Tina Jones or Tima Jones.

The command
grep 'ID [0-7][^T]00001' id-file
would search the id-file for any occurrences that matched 0-7 in the first byte, anything but T in the second byte, and 00001 as the remaining bytes.



Regular expressions and word begin / end and line begin / end


The next set of regular expression characters that can be used along with wildcard and regular characters allow you to match based on whether the expression is first or last in a word or line.

Ways to match these include:
  • \< : matches when a word begins with the expression following \<.
  • \> : matches when a word ends with the expression before \>.
  • ^ : matches when a line begins with the expression following ^.
  • $ : matches when a line ends with the expression before $.


Examples with grep:

The command
grep 'Fin$' the-little-prince
searches the file the-little-prince for any occurrences of "Fin" at the end of the line.

The command
grep '^Welcome to SuSE$' intro
searches the file intro for any occurrences of "Welcome to SuSE" on a line all by itself.

The command
grep '\<mac' inventory
searches the file inventory for any occurrences of a word beginning with the letters "mac…"



Regular expressions and repetition


Repetitions of a pattern can be matched for regular characters and wildcards.

Ways to match repetitions include:
  • * : matches when any number (including zero) of the character before it is found.
  • \{n\} : matches when exactly n of the character before it is found.
  • \{n,\} : matches when n or more of the character before it is found.
  • \{n,m\} : matches when n to m of the previous character is found.


Examples with grep:

The command
grep 'C*q' some-file
would search for any instances in some-file where C occurs zero or more times, followed by a q.

The command
grep '[a-w0-3]\{3,99\}' encrypted-file
would search for any instances where any of the characters a through w or any of the numbers 0 through 3 occur in encrypted-file from 3 to 99 times (e.g. "1111111", "abc123" would match).

The command
grep '[5][BC]*[1-9]' new-file
would search for any instances where a 5 is followed by zero or more instances of the letters B or C, followed by 1 through 9.


Credits:
anomie

Comments and Suggestions:
http://forums.suselinuxsupport.de/index.php?showtopic=23487

There are no comments on this page. [Add comment]


Warning: include(http://forums.suselinuxsupport.de/ssi.php?a=active) [function.include]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /home/customers/webs/slwiki/actions/footer.php on line 51

Warning: include() [function.include]: Failed opening 'http://forums.suselinuxsupport.de/ssi.php?a=active' for inclusion (include_path='.:/home/customers/webs') in /home/customers/webs/slwiki/actions/footer.php on line 51

Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by Wikka Wakka Wiki 1.1.6.0
Page was generated in 0.0268 seconds