PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/www/tags/NOV_07_2009/htdocs/users-guide/regexps.html

#
HTML | 63 lines | 63 code | 0 blank | 0 comment | 0 complexity | b7c6f09a9363438ea8b468e9d616e4a4 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Appendix E. Regular Expressions</title><meta name="generator" content="DocBook XSL Stylesheets V1.73.2"><link rel="start" href="index.html" title="jEdit 4.3 User's Guide"><link rel="up" href="using-jedit-part.html" title="Part I. Using jEdit"><link rel="prev" href="globs.html" title="Appendix D. Glob Patterns"><link rel="next" href="macro-index.html" title="Appendix F. Macros Included With jEdit"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Appendix E. Regular Expressions</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="globs.html">Prev</a> </td><th width="60%" align="center">Part I. Using jEdit</th><td width="20%" align="right"> <a accesskey="n" href="macro-index.html">Next</a></td></tr></table><hr></div><div class="appendix" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="regexps"></a>Appendix E. Regular Expressions</h2></div></div></div><p>jEdit 4.3pre5 and later uses regular expressions from <a class="ulink" href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html" target="_top">java.util.regex.Pattern</a>
  2. to implement inexact search and replace. Click there to see a complete
  3. reference guide to all supported meta-characters.</p><p>A regular expression consists of a string where some characters are
  4. given special meaning with regard to pattern matching.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Inside XML files</h3><p>Inside XML files (such as Jedit mode files), it is important that
  5. you escape XML special characters, such as &amp;, &lt;, &gt;, etc. You
  6. can use the XML plugin's "characters to entities" to perform this
  7. mapping.</p></div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Inside Java / beanshell / properties files</h3><p>Java strings are always parsed by java before they are processed
  8. by the regular expression engine, so you must make sure that backslashes
  9. are escaped by an extra backslash (<code class="literal">\\</code>)</p></div><p>Within a regular expression, the following characters have special
  10. meaning:</p><h4><a name="id2579273"></a>Positional Operators</h4><div class="itemizedlist"><ul type="disc"><li><p><code class="literal">^</code> matches at the beginning of a line</p></li><li><p><code class="literal">$</code> matches at the end of a line</p></li><li><p><code class="literal">\B</code> matches at a non-word break</p></li><li><p><code class="literal">\b</code> matches at a word boundary</p></li></ul></div><h4><a name="id2579327"></a>One-Character Operators</h4><div class="itemizedlist"><ul type="disc"><li><p><code class="literal">.</code> matches any single character</p></li><li><p><code class="literal">\d</code> matches any decimal digit</p></li><li><p><code class="literal">\D</code> matches any non-digit</p></li><li><p><code class="literal">\n</code> matches the newline character</p></li><li><p><code class="literal">\s</code> matches any whitespace character</p></li><li><p><code class="literal">\S</code> matches any non-whitespace
  11. character</p></li><li><p><code class="literal">\t</code> matches a horizontal tab
  12. character</p></li><li><p><code class="literal">\w</code> matches any word (alphanumeric)
  13. character</p></li><li><p><code class="literal">\W</code> matches any non-word (alphanumeric)
  14. character</p></li><li><p><code class="literal">\\</code> matches the backslash (&#8220;<span class="quote">\</span>&#8221;)
  15. character</p></li></ul></div><h4><a name="id2580272"></a>Character Class Operator</h4><div class="itemizedlist"><ul type="disc"><li><p><code class="literal">[<em class="replaceable"><code>abc</code></em>]</code> matches
  16. any character in the set <em class="replaceable"><code>a</code></em>,
  17. <em class="replaceable"><code>b</code></em> or <em class="replaceable"><code>c</code></em></p></li><li><p><code class="literal">[^<em class="replaceable"><code>abc</code></em>]</code> matches
  18. any character not in the set <em class="replaceable"><code>a</code></em>,
  19. <em class="replaceable"><code>b</code></em> or <em class="replaceable"><code>c</code></em></p></li><li><p><code class="literal">[<em class="replaceable"><code>a-z</code></em>]</code> matches
  20. any character in the range <em class="replaceable"><code>a</code></em> to
  21. <em class="replaceable"><code>z</code></em>, inclusive. A leading or trailing dash
  22. will be interpreted literally</p></li></ul></div><h4><a name="id2580352"></a>Subexpressions and Backreferences</h4><div class="itemizedlist"><ul type="disc"><li><p><code class="literal">(<em class="replaceable"><code>abc</code></em>)</code> matches
  23. whatever the expression <em class="replaceable"><code>abc</code></em> would match,
  24. and saves it as a subexpression. Also used for grouping</p></li><li><p><code class="literal">(?:<em class="replaceable"><code>...</code></em>)</code> pure
  25. grouping operator, does not save contents</p></li><li><p><code class="literal">(?#<em class="replaceable"><code>...</code></em>)</code> embedded
  26. comment, ignored by engine</p></li><li><p><code class="literal">(?=<em class="replaceable"><code>...</code></em>)</code> positive
  27. lookahead; the regular expression will match if the text in the
  28. brackets matches, but that text will not be considered part of the
  29. match</p></li><li><p><code class="literal">(?!<em class="replaceable"><code>...</code></em>)</code> negative
  30. lookahead; the regular expression will match if the text in the
  31. brackets does not match, and that text will not be considered part
  32. of the match</p></li><li><p><code class="literal">\<em class="replaceable"><code>n</code></em></code> where 0 &lt;
  33. <em class="replaceable"><code>n</code></em> &lt; 10, matches the same thing the
  34. <em class="replaceable"><code>n</code></em>th subexpression matched. Can only be
  35. used in the search string</p></li><li><p><code class="literal">$<em class="replaceable"><code>n</code></em></code> where 0 &lt;
  36. <em class="replaceable"><code>n</code></em> &lt; 10, substituted with the text
  37. matched by the <em class="replaceable"><code>n</code></em>th subexpression. Can
  38. only be used in the replacement string</p></li></ul></div><h4><a name="id2580494"></a>Branching (Alternation) Operator</h4><div class="itemizedlist"><ul type="disc"><li><p><code class="literal"><em class="replaceable"><code>a</code></em>|<em class="replaceable"><code>b</code></em></code>
  39. matches whatever the expression <em class="replaceable"><code>a</code></em> would
  40. match, or whatever the expression <em class="replaceable"><code>b</code></em> would
  41. match.</p></li></ul></div><h4><a name="id2580527"></a>Repeating Operators</h4><p>These symbols operate on the previous atomic expression.</p><div class="itemizedlist"><ul type="disc"><li><p><code class="literal">?</code> matches the preceding expression or the
  42. null string</p></li><li><p><code class="literal">*</code> matches the null string or any number of
  43. repetitions of the preceding expression</p></li><li><p><code class="literal">+</code> matches one or more repetitions of the
  44. preceding expression</p></li><li><p><code class="literal">{<em class="replaceable"><code>m</code></em>}</code> matches
  45. exactly <em class="replaceable"><code>m</code></em> repetitions of the
  46. one-character expression</p></li><li><p><code class="literal">{<em class="replaceable"><code>m</code></em>,<em class="replaceable"><code>n</code></em>}</code>
  47. matches between <em class="replaceable"><code>m</code></em> and
  48. <em class="replaceable"><code>n</code></em> repetitions of the preceding
  49. expression, inclusive</p></li><li><p><code class="literal">{<em class="replaceable"><code>m</code></em>,}</code> matches
  50. <em class="replaceable"><code>m</code></em> or more repetitions of the preceding
  51. expression</p></li></ul></div><h4><a name="id2580641"></a>Stingy (Minimal) Matching</h4><p>If a repeating operator (above) is immediately followed by a
  52. <code class="literal">?</code>, the repeating operator will stop at the smallest
  53. number of repetitions that can complete the rest of the match.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">On regex search</h3><p>There are some known issues with the
  54. <code class="literal">java.util.regex</code> library, as it stands in
  55. Sun's Java 1.5 and 1.6. In particular, it is possible to create
  56. regular expressions that hang the JVM, or cause stack overflow
  57. errors, which was not as easy to accomplish using the legacy
  58. <code class="literal">gnu.regexp</code> library. If you find that
  59. <code class="literal">gnu.regexp</code>, used in jEdit 4.2 and earlier, is
  60. more suitable for your search/replace needs, you can try the
  61. <span class="bold"><strong>XSearch plugin</strong></span>, which still
  62. uses it and can provide a replacement to the built-in search
  63. dialog.</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="globs.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="using-jedit-part.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="macro-index.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Appendix D. Glob Patterns </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Appendix F. Macros Included With jEdit</td></tr></table></div></body></html>