You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1500 lines
54 KiB

8 years ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta name="theme-color" content="#375EAB">
  7. <title>regexp - The Go Programming Language</title>
  8. <link type="text/css" rel="stylesheet" href="../../lib/godoc/style.css">
  9. <link rel="stylesheet" href="../../lib/godoc/jquery.treeview.css">
  10. <script type="text/javascript">window.initFuncs = [];</script>
  11. </head>
  12. <body>
  13. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  14. ...
  15. </div><!-- #lowframe -->
  16. <div id="topbar" class="wide"><div class="container">
  17. <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
  18. <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
  19. <a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  20. <form method="GET" action="http://localhost:6060/search">
  21. <div id="menu">
  22. <a href="http://localhost:6060/doc/">Documents</a>
  23. <a href="http://localhost:6060/pkg/">Packages</a>
  24. <a href="http://localhost:6060/project/">The Project</a>
  25. <a href="http://localhost:6060/help/">Help</a>
  26. <a href="http://localhost:6060/blog/">Blog</a>
  27. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  28. </div>
  29. </form>
  30. </div></div>
  31. <div id="page" class="wide">
  32. <div class="container">
  33. <h1>Package regexp</h1>
  34. <div id="nav"></div>
  35. <!--
  36. Copyright 2009 The Go Authors. All rights reserved.
  37. Use of this source code is governed by a BSD-style
  38. license that can be found in the LICENSE file.
  39. -->
  40. <!--
  41. Note: Static (i.e., not template-generated) href and id
  42. attributes start with "pkg-" to make it impossible for
  43. them to conflict with generated attributes (some of which
  44. correspond to Go identifiers).
  45. -->
  46. <script type='text/javascript'>
  47. document.ANALYSIS_DATA = null;
  48. document.CALLGRAPH = null;
  49. </script>
  50. <div id="short-nav">
  51. <dl>
  52. <dd><code>import "regexp"</code></dd>
  53. </dl>
  54. <dl>
  55. <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
  56. <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
  57. <dd><a href="index.html#pkg-examples" class="examplesLink">Examples</a></dd>
  58. <dd><a href="index.html#pkg-subdirectories">Subdirectories</a></dd>
  59. </dl>
  60. </div>
  61. <!-- The package's Name is printed as title by the top-level template -->
  62. <div id="pkg-overview" class="toggleVisible">
  63. <div class="collapsed">
  64. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  65. </div>
  66. <div class="expanded">
  67. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  68. <p>
  69. Package regexp implements regular expression search.
  70. </p>
  71. <p>
  72. The syntax of the regular expressions accepted is the same
  73. general syntax used by Perl, Python, and other languages.
  74. More precisely, it is the syntax accepted by RE2 and described at
  75. <a href="https://golang.org/s/re2syntax">https://golang.org/s/re2syntax</a>, except for \C.
  76. For an overview of the syntax, run
  77. </p>
  78. <pre>go doc regexp/syntax
  79. </pre>
  80. <p>
  81. The regexp implementation provided by this package is
  82. guaranteed to run in time linear in the size of the input.
  83. (This is a property not guaranteed by most open source
  84. implementations of regular expressions.) For more information
  85. about this property, see
  86. </p>
  87. <pre><a href="http://swtch.com/~rsc/regexp/regexp1.html">http://swtch.com/~rsc/regexp/regexp1.html</a>
  88. </pre>
  89. <p>
  90. or any book about automata theory.
  91. </p>
  92. <p>
  93. All characters are UTF-8-encoded code points.
  94. </p>
  95. <p>
  96. There are 16 methods of Regexp that match a regular expression and identify
  97. the matched text. Their names are matched by this regular expression:
  98. </p>
  99. <pre>Find(All)?(String)?(Submatch)?(Index)?
  100. </pre>
  101. <p>
  102. If &#39;All&#39; is present, the routine matches successive non-overlapping
  103. matches of the entire expression. Empty matches abutting a preceding
  104. match are ignored. The return value is a slice containing the successive
  105. return values of the corresponding non-&#39;All&#39; routine. These routines take
  106. an extra integer argument, n; if n &gt;= 0, the function returns at most n
  107. matches/submatches.
  108. </p>
  109. <p>
  110. If &#39;String&#39; is present, the argument is a string; otherwise it is a slice
  111. of bytes; return values are adjusted as appropriate.
  112. </p>
  113. <p>
  114. If &#39;Submatch&#39; is present, the return value is a slice identifying the
  115. successive submatches of the expression. Submatches are matches of
  116. parenthesized subexpressions (also known as capturing groups) within the
  117. regular expression, numbered from left to right in order of opening
  118. parenthesis. Submatch 0 is the match of the entire expression, submatch 1
  119. the match of the first parenthesized subexpression, and so on.
  120. </p>
  121. <p>
  122. If &#39;Index&#39; is present, matches and submatches are identified by byte index
  123. pairs within the input string: result[2*n:2*n+1] identifies the indexes of
  124. the nth submatch. The pair for n==0 identifies the match of the entire
  125. expression. If &#39;Index&#39; is not present, the match is identified by the
  126. text of the match/submatch. If an index is negative, it means that
  127. subexpression did not match any string in the input.
  128. </p>
  129. <p>
  130. There is also a subset of the methods that can be applied to text read
  131. from a RuneReader:
  132. </p>
  133. <pre>MatchReader, FindReaderIndex, FindReaderSubmatchIndex
  134. </pre>
  135. <p>
  136. This set may grow. Note that regular expression matches may need to
  137. examine text beyond the text returned by a match, so the methods that
  138. match text from a RuneReader may read arbitrarily far into the input
  139. before returning.
  140. </p>
  141. <p>
  142. (There are a few other methods that do not match this pattern.)
  143. </p>
  144. </div>
  145. </div>
  146. <div id="example_" class="toggle">
  147. <div class="collapsed">
  148. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  149. </div>
  150. <div class="expanded">
  151. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  152. <p>Code:</p>
  153. <pre class="code"><span class="comment">// Compile the expression once, usually at init time.</span>
  154. <span class="comment">// Use raw strings to avoid having to quote the backslashes.</span>
  155. var validID = regexp.MustCompile(`^[a-z]+\[[0-9]+\]$`)
  156. fmt.Println(validID.MatchString(&#34;adam[23]&#34;))
  157. fmt.Println(validID.MatchString(&#34;eve[7]&#34;))
  158. fmt.Println(validID.MatchString(&#34;Job[48]&#34;))
  159. fmt.Println(validID.MatchString(&#34;snakey&#34;))
  160. <span class="comment"></pre>
  161. <p>Output:</p>
  162. <pre class="output">true
  163. true
  164. false
  165. false
  166. </pre>
  167. </div>
  168. </div>
  169. <div id="pkg-index" class="toggleVisible">
  170. <div class="collapsed">
  171. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  172. </div>
  173. <div class="expanded">
  174. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  175. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  176. <div id="manual-nav">
  177. <dl>
  178. <dd><a href="index.html#Match">func Match(pattern string, b []byte) (matched bool, err error)</a></dd>
  179. <dd><a href="index.html#MatchReader">func MatchReader(pattern string, r io.RuneReader) (matched bool, err error)</a></dd>
  180. <dd><a href="index.html#MatchString">func MatchString(pattern string, s string) (matched bool, err error)</a></dd>
  181. <dd><a href="index.html#QuoteMeta">func QuoteMeta(s string) string</a></dd>
  182. <dd><a href="index.html#Regexp">type Regexp</a></dd>
  183. <dd>&nbsp; &nbsp; <a href="index.html#Compile">func Compile(expr string) (*Regexp, error)</a></dd>
  184. <dd>&nbsp; &nbsp; <a href="index.html#CompilePOSIX">func CompilePOSIX(expr string) (*Regexp, error)</a></dd>
  185. <dd>&nbsp; &nbsp; <a href="index.html#MustCompile">func MustCompile(str string) *Regexp</a></dd>
  186. <dd>&nbsp; &nbsp; <a href="index.html#MustCompilePOSIX">func MustCompilePOSIX(str string) *Regexp</a></dd>
  187. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.Copy">func (re *Regexp) Copy() *Regexp</a></dd>
  188. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.Expand">func (re *Regexp) Expand(dst []byte, template []byte, src []byte, match []int) []byte</a></dd>
  189. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.ExpandString">func (re *Regexp) ExpandString(dst []byte, template string, src string, match []int) []byte</a></dd>
  190. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.Find">func (re *Regexp) Find(b []byte) []byte</a></dd>
  191. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.FindAll">func (re *Regexp) FindAll(b []byte, n int) [][]byte</a></dd>
  192. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.FindAllIndex">func (re *Regexp) FindAllIndex(b []byte, n int) [][]int</a></dd>
  193. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.FindAllString">func (re *Regexp) FindAllString(s string, n int) []string</a></dd>
  194. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.FindAllStringIndex">func (re *Regexp) FindAllStringIndex(s string, n int) [][]int</a></dd>
  195. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.FindAllStringSubmatch">func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string</a></dd>
  196. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.FindAllStringSubmatchIndex">func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int</a></dd>
  197. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.FindAllSubmatch">func (re *Regexp) FindAllSubmatch(b []byte, n int) [][][]byte</a></dd>
  198. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.FindAllSubmatchIndex">func (re *Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int</a></dd>
  199. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.FindIndex">func (re *Regexp) FindIndex(b []byte) (loc []int)</a></dd>
  200. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.FindReaderIndex">func (re *Regexp) FindReaderIndex(r io.RuneReader) (loc []int)</a></dd>
  201. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.FindReaderSubmatchIndex">func (re *Regexp) FindReaderSubmatchIndex(r io.RuneReader) []int</a></dd>
  202. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.FindString">func (re *Regexp) FindString(s string) string</a></dd>
  203. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.FindStringIndex">func (re *Regexp) FindStringIndex(s string) (loc []int)</a></dd>
  204. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.FindStringSubmatch">func (re *Regexp) FindStringSubmatch(s string) []string</a></dd>
  205. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.FindStringSubmatchIndex">func (re *Regexp) FindStringSubmatchIndex(s string) []int</a></dd>
  206. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.FindSubmatch">func (re *Regexp) FindSubmatch(b []byte) [][]byte</a></dd>
  207. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.FindSubmatchIndex">func (re *Regexp) FindSubmatchIndex(b []byte) []int</a></dd>
  208. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.LiteralPrefix">func (re *Regexp) LiteralPrefix() (prefix string, complete bool)</a></dd>
  209. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.Longest">func (re *Regexp) Longest()</a></dd>
  210. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.Match">func (re *Regexp) Match(b []byte) bool</a></dd>
  211. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.MatchReader">func (re *Regexp) MatchReader(r io.RuneReader) bool</a></dd>
  212. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.MatchString">func (re *Regexp) MatchString(s string) bool</a></dd>
  213. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.NumSubexp">func (re *Regexp) NumSubexp() int</a></dd>
  214. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.ReplaceAll">func (re *Regexp) ReplaceAll(src, repl []byte) []byte</a></dd>
  215. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.ReplaceAllFunc">func (re *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte</a></dd>
  216. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.ReplaceAllLiteral">func (re *Regexp) ReplaceAllLiteral(src, repl []byte) []byte</a></dd>
  217. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.ReplaceAllLiteralString">func (re *Regexp) ReplaceAllLiteralString(src, repl string) string</a></dd>
  218. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.ReplaceAllString">func (re *Regexp) ReplaceAllString(src, repl string) string</a></dd>
  219. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.ReplaceAllStringFunc">func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string</a></dd>
  220. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.Split">func (re *Regexp) Split(s string, n int) []string</a></dd>
  221. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.String">func (re *Regexp) String() string</a></dd>
  222. <dd>&nbsp; &nbsp; <a href="index.html#Regexp.SubexpNames">func (re *Regexp) SubexpNames() []string</a></dd>
  223. </dl>
  224. </div><!-- #manual-nav -->
  225. <div id="pkg-examples">
  226. <h4>Examples</h4>
  227. <dl>
  228. <dd><a class="exampleLink" href="index.html#example_">Package</a></dd>
  229. <dd><a class="exampleLink" href="index.html#example_MatchString">MatchString</a></dd>
  230. <dd><a class="exampleLink" href="index.html#example_Regexp_FindAllString">Regexp.FindAllString</a></dd>
  231. <dd><a class="exampleLink" href="index.html#example_Regexp_FindAllStringSubmatch">Regexp.FindAllStringSubmatch</a></dd>
  232. <dd><a class="exampleLink" href="index.html#example_Regexp_FindAllStringSubmatchIndex">Regexp.FindAllStringSubmatchIndex</a></dd>
  233. <dd><a class="exampleLink" href="index.html#example_Regexp_FindString">Regexp.FindString</a></dd>
  234. <dd><a class="exampleLink" href="index.html#example_Regexp_FindStringIndex">Regexp.FindStringIndex</a></dd>
  235. <dd><a class="exampleLink" href="index.html#example_Regexp_FindStringSubmatch">Regexp.FindStringSubmatch</a></dd>
  236. <dd><a class="exampleLink" href="index.html#example_Regexp_ReplaceAllLiteralString">Regexp.ReplaceAllLiteralString</a></dd>
  237. <dd><a class="exampleLink" href="index.html#example_Regexp_ReplaceAllString">Regexp.ReplaceAllString</a></dd>
  238. <dd><a class="exampleLink" href="index.html#example_Regexp_Split">Regexp.Split</a></dd>
  239. <dd><a class="exampleLink" href="index.html#example_Regexp_SubexpNames">Regexp.SubexpNames</a></dd>
  240. </dl>
  241. </div>
  242. <h4>Package files</h4>
  243. <p>
  244. <span style="font-size:90%">
  245. <a href="http://localhost:6060/src/regexp/backtrack.go">backtrack.go</a>
  246. <a href="http://localhost:6060/src/regexp/exec.go">exec.go</a>
  247. <a href="http://localhost:6060/src/regexp/onepass.go">onepass.go</a>
  248. <a href="http://localhost:6060/src/regexp/regexp.go">regexp.go</a>
  249. </span>
  250. </p>
  251. </div><!-- .expanded -->
  252. </div><!-- #pkg-index -->
  253. <div id="pkg-callgraph" class="toggle" style="display: none">
  254. <div class="collapsed">
  255. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  256. </div> <!-- .expanded -->
  257. <div class="expanded">
  258. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  259. <p>
  260. In the call graph viewer below, each node
  261. is a function belonging to this package
  262. and its children are the functions it
  263. calls&mdash;perhaps dynamically.
  264. </p>
  265. <p>
  266. The root nodes are the entry points of the
  267. package: functions that may be called from
  268. outside the package.
  269. There may be non-exported or anonymous
  270. functions among them if they are called
  271. dynamically from another package.
  272. </p>
  273. <p>
  274. Click a node to visit that function's source code.
  275. From there you can visit its callers by
  276. clicking its declaring <code>func</code>
  277. token.
  278. </p>
  279. <p>
  280. Functions may be omitted if they were
  281. determined to be unreachable in the
  282. particular programs or tests that were
  283. analyzed.
  284. </p>
  285. <!-- Zero means show all package entry points. -->
  286. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  287. </div>
  288. </div> <!-- #pkg-callgraph -->
  289. <h2 id="Match">func <a href="http://localhost:6060/src/regexp/regexp.go?s=13933:13995#L434">Match</a></h2>
  290. <pre>func Match(pattern <a href="../builtin/index.html#string">string</a>, b []<a href="../builtin/index.html#byte">byte</a>) (matched <a href="../builtin/index.html#bool">bool</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  291. <p>
  292. Match checks whether a textual regular expression
  293. matches a byte slice. More complicated queries need
  294. to use Compile and the full Regexp interface.
  295. </p>
  296. <h2 id="MatchReader">func <a href="http://localhost:6060/src/regexp/regexp.go?s=13260:13335#L412">MatchReader</a></h2>
  297. <pre>func MatchReader(pattern <a href="../builtin/index.html#string">string</a>, r <a href="../io/index.html">io</a>.<a href="../io/index.html#RuneReader">RuneReader</a>) (matched <a href="../builtin/index.html#bool">bool</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  298. <p>
  299. MatchReader checks whether a textual regular expression matches the text
  300. read by the RuneReader. More complicated queries need to use Compile and
  301. the full Regexp interface.
  302. </p>
  303. <h2 id="MatchString">func <a href="http://localhost:6060/src/regexp/regexp.go?s=13601:13669#L423">MatchString</a></h2>
  304. <pre>func MatchString(pattern <a href="../builtin/index.html#string">string</a>, s <a href="../builtin/index.html#string">string</a>) (matched <a href="../builtin/index.html#bool">bool</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  305. <p>
  306. MatchString checks whether a textual regular expression
  307. matches a string. More complicated queries need
  308. to use Compile and the full Regexp interface.
  309. </p>
  310. <div id="example_MatchString" class="toggle">
  311. <div class="collapsed">
  312. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  313. </div>
  314. <div class="expanded">
  315. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  316. <p>Code:</p>
  317. <pre class="code">matched, err := regexp.MatchString(&#34;foo.*&#34;, &#34;seafood&#34;)
  318. fmt.Println(matched, err)
  319. matched, err = regexp.MatchString(&#34;bar.*&#34;, &#34;seafood&#34;)
  320. fmt.Println(matched, err)
  321. matched, err = regexp.MatchString(&#34;a(b&#34;, &#34;seafood&#34;)
  322. fmt.Println(matched, err)
  323. <span class="comment"></pre>
  324. <p>Output:</p>
  325. <pre class="output">true &lt;nil&gt;
  326. false &lt;nil&gt;
  327. false error parsing regexp: missing closing ): `a(b`
  328. </pre>
  329. </div>
  330. </div>
  331. <h2 id="QuoteMeta">func <a href="http://localhost:6060/src/regexp/regexp.go?s=18945:18976#L586">QuoteMeta</a></h2>
  332. <pre>func QuoteMeta(s <a href="../builtin/index.html#string">string</a>) <a href="../builtin/index.html#string">string</a></pre>
  333. <p>
  334. QuoteMeta returns a string that quotes all regular expression metacharacters
  335. inside the argument text; the returned string is a regular expression matching
  336. the literal text. For example, QuoteMeta(`[foo]`) returns `\[foo\]`.
  337. </p>
  338. <h2 id="Regexp">type <a href="http://localhost:6060/src/regexp/regexp.go?s=3279:4028#L72">Regexp</a></h2>
  339. <pre>type Regexp struct {
  340. <span class="comment">// contains filtered or unexported fields</span>
  341. }</pre>
  342. <p>
  343. Regexp is the representation of a compiled regular expression.
  344. A Regexp is safe for concurrent use by multiple goroutines.
  345. </p>
  346. <h3 id="Compile">func <a href="http://localhost:6060/src/regexp/regexp.go?s=5016:5058#L118">Compile</a></h3>
  347. <pre>func Compile(expr <a href="../builtin/index.html#string">string</a>) (*<a href="index.html#Regexp">Regexp</a>, <a href="../builtin/index.html#error">error</a>)</pre>
  348. <p>
  349. Compile parses a regular expression and returns, if successful,
  350. a Regexp object that can be used to match against text.
  351. </p>
  352. <p>
  353. When matching against text, the regexp returns a match that
  354. begins as early as possible in the input (leftmost), and among those
  355. it chooses the one that a backtracking search would have found first.
  356. This so-called leftmost-first matching is the same semantics
  357. that Perl, Python, and other implementations use, although this
  358. package implements it without the expense of backtracking.
  359. For POSIX leftmost-longest matching, see CompilePOSIX.
  360. </p>
  361. <h3 id="CompilePOSIX">func <a href="http://localhost:6060/src/regexp/regexp.go?s=6177:6224#L141">CompilePOSIX</a></h3>
  362. <pre>func CompilePOSIX(expr <a href="../builtin/index.html#string">string</a>) (*<a href="index.html#Regexp">Regexp</a>, <a href="../builtin/index.html#error">error</a>)</pre>
  363. <p>
  364. CompilePOSIX is like Compile but restricts the regular expression
  365. to POSIX ERE (egrep) syntax and changes the match semantics to
  366. leftmost-longest.
  367. </p>
  368. <p>
  369. That is, when matching against text, the regexp returns a match that
  370. begins as early as possible in the input (leftmost), and among those
  371. it chooses a match that is as long as possible.
  372. This so-called leftmost-longest matching is the same semantics
  373. that early regular expression implementations used and that POSIX
  374. specifies.
  375. </p>
  376. <p>
  377. However, there can be multiple leftmost-longest matches, with different
  378. submatch choices, and here this package diverges from POSIX.
  379. Among the possible leftmost-longest matches, this package chooses
  380. the one that a backtracking search would have found first, while POSIX
  381. specifies that the match be chosen to maximize the length of the first
  382. subexpression, then the second, and so on from left to right.
  383. The POSIX rule is computationally prohibitive and not even well-defined.
  384. See <a href="http://swtch.com/~rsc/regexp/regexp2.html#posix">http://swtch.com/~rsc/regexp/regexp2.html#posix</a> for details.
  385. </p>
  386. <h3 id="MustCompile">func <a href="http://localhost:6060/src/regexp/regexp.go?s=8419:8455#L219">MustCompile</a></h3>
  387. <pre>func MustCompile(str <a href="../builtin/index.html#string">string</a>) *<a href="index.html#Regexp">Regexp</a></pre>
  388. <p>
  389. MustCompile is like Compile but panics if the expression cannot be parsed.
  390. It simplifies safe initialization of global variables holding compiled regular
  391. expressions.
  392. </p>
  393. <h3 id="MustCompilePOSIX">func <a href="http://localhost:6060/src/regexp/regexp.go?s=8780:8821#L230">MustCompilePOSIX</a></h3>
  394. <pre>func MustCompilePOSIX(str <a href="../builtin/index.html#string">string</a>) *<a href="index.html#Regexp">Regexp</a></pre>
  395. <p>
  396. MustCompilePOSIX is like CompilePOSIX but panics if the expression cannot be parsed.
  397. It simplifies safe initialization of global variables holding compiled regular
  398. expressions.
  399. </p>
  400. <h3 id="Regexp.Copy">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=4331:4363#L101">Copy</a></h3>
  401. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) Copy() *<a href="index.html#Regexp">Regexp</a></pre>
  402. <p>
  403. Copy returns a new Regexp object copied from re.
  404. </p>
  405. <p>
  406. When using a Regexp in multiple goroutines, giving each goroutine
  407. its own copy helps to avoid lock contention.
  408. </p>
  409. <h3 id="Regexp.Expand">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=24117:24202#L762">Expand</a></h3>
  410. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) Expand(dst []<a href="../builtin/index.html#byte">byte</a>, template []<a href="../builtin/index.html#byte">byte</a>, src []<a href="../builtin/index.html#byte">byte</a>, match []<a href="../builtin/index.html#int">int</a>) []<a href="../builtin/index.html#byte">byte</a></pre>
  411. <p>
  412. Expand appends template to dst and returns the result; during the
  413. append, Expand replaces variables in the template with corresponding
  414. matches drawn from src. The match slice should have been returned by
  415. FindSubmatchIndex.
  416. </p>
  417. <p>
  418. In the template, a variable is denoted by a substring of the form
  419. $name or ${name}, where name is a non-empty sequence of letters,
  420. digits, and underscores. A purely numeric name like $1 refers to
  421. the submatch with the corresponding index; other names refer to
  422. capturing parentheses named with the (?P&lt;name&gt;...) syntax. A
  423. reference to an out of range or unmatched index or a name that is not
  424. present in the regular expression is replaced with an empty slice.
  425. </p>
  426. <p>
  427. In the $name form, name is taken to be as long as possible: $1x is
  428. equivalent to ${1x}, not ${1}x, and, $10 is equivalent to ${10}, not ${1}0.
  429. </p>
  430. <p>
  431. To insert a literal $ in the output, use $$ in the template.
  432. </p>
  433. <h3 id="Regexp.ExpandString">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=24441:24532#L769">ExpandString</a></h3>
  434. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) ExpandString(dst []<a href="../builtin/index.html#byte">byte</a>, template <a href="../builtin/index.html#string">string</a>, src <a href="../builtin/index.html#string">string</a>, match []<a href="../builtin/index.html#int">int</a>) []<a href="../builtin/index.html#byte">byte</a></pre>
  435. <p>
  436. ExpandString is like Expand but the template and source are strings.
  437. It appends to and returns a byte slice in order to give the calling
  438. code control over allocation.
  439. </p>
  440. <h3 id="Regexp.Find">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=20801:20840#L668">Find</a></h3>
  441. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) Find(b []<a href="../builtin/index.html#byte">byte</a>) []<a href="../builtin/index.html#byte">byte</a></pre>
  442. <p>
  443. Find returns a slice holding the text of the leftmost match in b of the regular expression.
  444. A return value of nil indicates no match.
  445. </p>
  446. <h3 id="Regexp.FindAll">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=28946:28997#L925">FindAll</a></h3>
  447. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) FindAll(b []<a href="../builtin/index.html#byte">byte</a>, n <a href="../builtin/index.html#int">int</a>) [][]<a href="../builtin/index.html#byte">byte</a></pre>
  448. <p>
  449. FindAll is the &#39;All&#39; version of Find; it returns a slice of all successive
  450. matches of the expression, as defined by the &#39;All&#39; description in the
  451. package comment.
  452. A return value of nil indicates no match.
  453. </p>
  454. <h3 id="Regexp.FindAllIndex">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=29452:29507#L943">FindAllIndex</a></h3>
  455. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) FindAllIndex(b []<a href="../builtin/index.html#byte">byte</a>, n <a href="../builtin/index.html#int">int</a>) [][]<a href="../builtin/index.html#int">int</a></pre>
  456. <p>
  457. FindAllIndex is the &#39;All&#39; version of FindIndex; it returns a slice of all
  458. successive matches of the expression, as defined by the &#39;All&#39; description
  459. in the package comment.
  460. A return value of nil indicates no match.
  461. </p>
  462. <h3 id="Regexp.FindAllString">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=29953:30010#L961">FindAllString</a></h3>
  463. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) FindAllString(s <a href="../builtin/index.html#string">string</a>, n <a href="../builtin/index.html#int">int</a>) []<a href="../builtin/index.html#string">string</a></pre>
  464. <p>
  465. FindAllString is the &#39;All&#39; version of FindString; it returns a slice of all
  466. successive matches of the expression, as defined by the &#39;All&#39; description
  467. in the package comment.
  468. A return value of nil indicates no match.
  469. </p>
  470. <div id="example_Regexp_FindAllString" class="toggle">
  471. <div class="collapsed">
  472. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  473. </div>
  474. <div class="expanded">
  475. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  476. <p>Code:</p>
  477. <pre class="code">re := regexp.MustCompile(&#34;a.&#34;)
  478. fmt.Println(re.FindAllString(&#34;paranormal&#34;, -1))
  479. fmt.Println(re.FindAllString(&#34;paranormal&#34;, 2))
  480. fmt.Println(re.FindAllString(&#34;graal&#34;, -1))
  481. fmt.Println(re.FindAllString(&#34;none&#34;, -1))
  482. <span class="comment"></pre>
  483. <p>Output:</p>
  484. <pre class="output">[ar an al]
  485. [ar an]
  486. [aa]
  487. []
  488. </pre>
  489. </div>
  490. </div>
  491. <h3 id="Regexp.FindAllStringIndex">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=30478:30539#L979">FindAllStringIndex</a></h3>
  492. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) FindAllStringIndex(s <a href="../builtin/index.html#string">string</a>, n <a href="../builtin/index.html#int">int</a>) [][]<a href="../builtin/index.html#int">int</a></pre>
  493. <p>
  494. FindAllStringIndex is the &#39;All&#39; version of FindStringIndex; it returns a
  495. slice of all successive matches of the expression, as defined by the &#39;All&#39;
  496. description in the package comment.
  497. A return value of nil indicates no match.
  498. </p>
  499. <h3 id="Regexp.FindAllStringSubmatch">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=32169:32236#L1039">FindAllStringSubmatch</a></h3>
  500. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) FindAllStringSubmatch(s <a href="../builtin/index.html#string">string</a>, n <a href="../builtin/index.html#int">int</a>) [][]<a href="../builtin/index.html#string">string</a></pre>
  501. <p>
  502. FindAllStringSubmatch is the &#39;All&#39; version of FindStringSubmatch; it
  503. returns a slice of all successive matches of the expression, as defined by
  504. the &#39;All&#39; description in the package comment.
  505. A return value of nil indicates no match.
  506. </p>
  507. <div id="example_Regexp_FindAllStringSubmatch" class="toggle">
  508. <div class="collapsed">
  509. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  510. </div>
  511. <div class="expanded">
  512. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  513. <p>Code:</p>
  514. <pre class="code">re := regexp.MustCompile(&#34;a(x*)b&#34;)
  515. fmt.Printf(&#34;%q\n&#34;, re.FindAllStringSubmatch(&#34;-ab-&#34;, -1))
  516. fmt.Printf(&#34;%q\n&#34;, re.FindAllStringSubmatch(&#34;-axxb-&#34;, -1))
  517. fmt.Printf(&#34;%q\n&#34;, re.FindAllStringSubmatch(&#34;-ab-axb-&#34;, -1))
  518. fmt.Printf(&#34;%q\n&#34;, re.FindAllStringSubmatch(&#34;-axxb-ab-&#34;, -1))
  519. <span class="comment"></pre>
  520. <p>Output:</p>
  521. <pre class="output">[[&#34;ab&#34; &#34;&#34;]]
  522. [[&#34;axxb&#34; &#34;xx&#34;]]
  523. [[&#34;ab&#34; &#34;&#34;] [&#34;axb&#34; &#34;x&#34;]]
  524. [[&#34;axxb&#34; &#34;xx&#34;] [&#34;ab&#34; &#34;&#34;]]
  525. </pre>
  526. </div>
  527. </div>
  528. <h3 id="Regexp.FindAllStringSubmatchIndex">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=32850:32919#L1064">FindAllStringSubmatchIndex</a></h3>
  529. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) FindAllStringSubmatchIndex(s <a href="../builtin/index.html#string">string</a>, n <a href="../builtin/index.html#int">int</a>) [][]<a href="../builtin/index.html#int">int</a></pre>
  530. <p>
  531. FindAllStringSubmatchIndex is the &#39;All&#39; version of
  532. FindStringSubmatchIndex; it returns a slice of all successive matches of
  533. the expression, as defined by the &#39;All&#39; description in the package
  534. comment.
  535. A return value of nil indicates no match.
  536. </p>
  537. <div id="example_Regexp_FindAllStringSubmatchIndex" class="toggle">
  538. <div class="collapsed">
  539. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  540. </div>
  541. <div class="expanded">
  542. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  543. <p>Code:</p>
  544. <pre class="code">re := regexp.MustCompile(&#34;a(x*)b&#34;)
  545. <span class="comment">// Indices:</span>
  546. <span class="comment">// 01234567 012345678</span>
  547. <span class="comment">// -ab-axb- -axxb-ab-</span>
  548. fmt.Println(re.FindAllStringSubmatchIndex(&#34;-ab-&#34;, -1))
  549. fmt.Println(re.FindAllStringSubmatchIndex(&#34;-axxb-&#34;, -1))
  550. fmt.Println(re.FindAllStringSubmatchIndex(&#34;-ab-axb-&#34;, -1))
  551. fmt.Println(re.FindAllStringSubmatchIndex(&#34;-axxb-ab-&#34;, -1))
  552. fmt.Println(re.FindAllStringSubmatchIndex(&#34;-foo-&#34;, -1))
  553. <span class="comment"></pre>
  554. <p>Output:</p>
  555. <pre class="output">[[1 3 2 2]]
  556. [[1 5 2 4]]
  557. [[1 3 2 2] [4 7 5 6]]
  558. [[1 5 2 4] [6 8 7 7]]
  559. []
  560. </pre>
  561. </div>
  562. </div>
  563. <h3 id="Regexp.FindAllSubmatch">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=30990:31051#L997">FindAllSubmatch</a></h3>
  564. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) FindAllSubmatch(b []<a href="../builtin/index.html#byte">byte</a>, n <a href="../builtin/index.html#int">int</a>) [][][]<a href="../builtin/index.html#byte">byte</a></pre>
  565. <p>
  566. FindAllSubmatch is the &#39;All&#39; version of FindSubmatch; it returns a slice
  567. of all successive matches of the expression, as defined by the &#39;All&#39;
  568. description in the package comment.
  569. A return value of nil indicates no match.
  570. </p>
  571. <h3 id="Regexp.FindAllSubmatchIndex">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=31649:31712#L1021">FindAllSubmatchIndex</a></h3>
  572. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) FindAllSubmatchIndex(b []<a href="../builtin/index.html#byte">byte</a>, n <a href="../builtin/index.html#int">int</a>) [][]<a href="../builtin/index.html#int">int</a></pre>
  573. <p>
  574. FindAllSubmatchIndex is the &#39;All&#39; version of FindSubmatchIndex; it returns
  575. a slice of all successive matches of the expression, as defined by the
  576. &#39;All&#39; description in the package comment.
  577. A return value of nil indicates no match.
  578. </p>
  579. <h3 id="Regexp.FindIndex">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=21157:21206#L680">FindIndex</a></h3>
  580. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) FindIndex(b []<a href="../builtin/index.html#byte">byte</a>) (loc []<a href="../builtin/index.html#int">int</a>)</pre>
  581. <p>
  582. FindIndex returns a two-element slice of integers defining the location of
  583. the leftmost match in b of the regular expression. The match itself is at
  584. b[loc[0]:loc[1]].
  585. A return value of nil indicates no match.
  586. </p>
  587. <h3 id="Regexp.FindReaderIndex">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=22465:22527#L718">FindReaderIndex</a></h3>
  588. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) FindReaderIndex(r <a href="../io/index.html">io</a>.<a href="../io/index.html#RuneReader">RuneReader</a>) (loc []<a href="../builtin/index.html#int">int</a>)</pre>
  589. <p>
  590. FindReaderIndex returns a two-element slice of integers defining the
  591. location of the leftmost match of the regular expression in text read from
  592. the RuneReader. The match text was found in the input stream at
  593. byte offset loc[0] through loc[1]-1.
  594. A return value of nil indicates no match.
  595. </p>
  596. <h3 id="Regexp.FindReaderSubmatchIndex">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=28517:28581#L915">FindReaderSubmatchIndex</a></h3>
  597. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) FindReaderSubmatchIndex(r <a href="../io/index.html">io</a>.<a href="../io/index.html#RuneReader">RuneReader</a>) []<a href="../builtin/index.html#int">int</a></pre>
  598. <p>
  599. FindReaderSubmatchIndex returns a slice holding the index pairs
  600. identifying the leftmost match of the regular expression of text read by
  601. the RuneReader, and the matches, if any, of its subexpressions, as defined
  602. by the &#39;Submatch&#39; and &#39;Index&#39; descriptions in the package comment. A
  603. return value of nil indicates no match.
  604. </p>
  605. <h3 id="Regexp.FindString">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=21649:21694#L693">FindString</a></h3>
  606. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) FindString(s <a href="../builtin/index.html#string">string</a>) <a href="../builtin/index.html#string">string</a></pre>
  607. <p>
  608. FindString returns a string holding the text of the leftmost match in s of the regular
  609. expression. If there is no match, the return value is an empty string,
  610. but it will also be empty if the regular expression successfully matches
  611. an empty string. Use FindStringIndex or FindStringSubmatch if it is
  612. necessary to distinguish these cases.
  613. </p>
  614. <div id="example_Regexp_FindString" class="toggle">
  615. <div class="collapsed">
  616. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  617. </div>
  618. <div class="expanded">
  619. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  620. <p>Code:</p>
  621. <pre class="code">re := regexp.MustCompile(&#34;fo.?&#34;)
  622. fmt.Printf(&#34;%q\n&#34;, re.FindString(&#34;seafood&#34;))
  623. fmt.Printf(&#34;%q\n&#34;, re.FindString(&#34;meat&#34;))
  624. <span class="comment"></pre>
  625. <p>Output:</p>
  626. <pre class="output">&#34;foo&#34;
  627. &#34;&#34;
  628. </pre>
  629. </div>
  630. </div>
  631. <h3 id="Regexp.FindStringIndex">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=22017:22072#L705">FindStringIndex</a></h3>
  632. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) FindStringIndex(s <a href="../builtin/index.html#string">string</a>) (loc []<a href="../builtin/index.html#int">int</a>)</pre>
  633. <p>
  634. FindStringIndex returns a two-element slice of integers defining the
  635. location of the leftmost match in s of the regular expression. The match
  636. itself is at s[loc[0]:loc[1]].
  637. A return value of nil indicates no match.
  638. </p>
  639. <div id="example_Regexp_FindStringIndex" class="toggle">
  640. <div class="collapsed">
  641. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  642. </div>
  643. <div class="expanded">
  644. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  645. <p>Code:</p>
  646. <pre class="code">re := regexp.MustCompile(&#34;ab?&#34;)
  647. fmt.Println(re.FindStringIndex(&#34;tablett&#34;))
  648. fmt.Println(re.FindStringIndex(&#34;foo&#34;) == nil)
  649. <span class="comment"></pre>
  650. <p>Output:</p>
  651. <pre class="output">[1 3]
  652. true
  653. </pre>
  654. </div>
  655. </div>
  656. <h3 id="Regexp.FindStringSubmatch">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=27458:27513#L887">FindStringSubmatch</a></h3>
  657. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) FindStringSubmatch(s <a href="../builtin/index.html#string">string</a>) []<a href="../builtin/index.html#string">string</a></pre>
  658. <p>
  659. FindStringSubmatch returns a slice of strings holding the text of the
  660. leftmost match of the regular expression in s and the matches, if any, of
  661. its subexpressions, as defined by the &#39;Submatch&#39; description in the
  662. package comment.
  663. A return value of nil indicates no match.
  664. </p>
  665. <div id="example_Regexp_FindStringSubmatch" class="toggle">
  666. <div class="collapsed">
  667. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  668. </div>
  669. <div class="expanded">
  670. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  671. <p>Code:</p>
  672. <pre class="code">re := regexp.MustCompile(&#34;a(x*)b(y|z)c&#34;)
  673. fmt.Printf(&#34;%q\n&#34;, re.FindStringSubmatch(&#34;-axxxbyc-&#34;))
  674. fmt.Printf(&#34;%q\n&#34;, re.FindStringSubmatch(&#34;-abzc-&#34;))
  675. <span class="comment"></pre>
  676. <p>Output:</p>
  677. <pre class="output">[&#34;axxxbyc&#34; &#34;xxx&#34; &#34;y&#34;]
  678. [&#34;abzc&#34; &#34;&#34; &#34;z&#34;]
  679. </pre>
  680. </div>
  681. </div>
  682. <h3 id="Regexp.FindStringSubmatchIndex">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=28056:28113#L906">FindStringSubmatchIndex</a></h3>
  683. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) FindStringSubmatchIndex(s <a href="../builtin/index.html#string">string</a>) []<a href="../builtin/index.html#int">int</a></pre>
  684. <p>
  685. FindStringSubmatchIndex returns a slice holding the index pairs
  686. identifying the leftmost match of the regular expression in s and the
  687. matches, if any, of its subexpressions, as defined by the &#39;Submatch&#39; and
  688. &#39;Index&#39; descriptions in the package comment.
  689. A return value of nil indicates no match.
  690. </p>
  691. <h3 id="Regexp.FindSubmatch">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=22896:22945#L731">FindSubmatch</a></h3>
  692. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) FindSubmatch(b []<a href="../builtin/index.html#byte">byte</a>) [][]<a href="../builtin/index.html#byte">byte</a></pre>
  693. <p>
  694. FindSubmatch returns a slice of slices holding the text of the leftmost
  695. match of the regular expression in b and the matches, if any, of its
  696. subexpressions, as defined by the &#39;Submatch&#39; descriptions in the package
  697. comment.
  698. A return value of nil indicates no match.
  699. </p>
  700. <h3 id="Regexp.FindSubmatchIndex">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=27055:27106#L878">FindSubmatchIndex</a></h3>
  701. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) FindSubmatchIndex(b []<a href="../builtin/index.html#byte">byte</a>) []<a href="../builtin/index.html#int">int</a></pre>
  702. <p>
  703. FindSubmatchIndex returns a slice holding the index pairs identifying the
  704. leftmost match of the regular expression in b and the matches, if any, of
  705. its subexpressions, as defined by the &#39;Submatch&#39; and &#39;Index&#39; descriptions
  706. in the package comment.
  707. A return value of nil indicates no match.
  708. </p>
  709. <h3 id="Regexp.LiteralPrefix">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=12468:12532#L389">LiteralPrefix</a></h3>
  710. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) LiteralPrefix() (prefix <a href="../builtin/index.html#string">string</a>, complete <a href="../builtin/index.html#bool">bool</a>)</pre>
  711. <p>
  712. LiteralPrefix returns a literal string that must begin any match
  713. of the regular expression re. It returns the boolean true if the
  714. literal string comprises the entire regular expression.
  715. </p>
  716. <h3 id="Regexp.Longest">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=6535:6562#L149">Longest</a></h3>
  717. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) Longest()</pre>
  718. <p>
  719. Longest makes future searches prefer the leftmost-longest match.
  720. That is, when matching against text, the regexp returns a match that
  721. begins as early as possible in the input (leftmost), and among those
  722. it chooses a match that is as long as possible.
  723. </p>
  724. <h3 id="Regexp.Match">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=12987:13025#L405">Match</a></h3>
  725. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) Match(b []<a href="../builtin/index.html#byte">byte</a>) <a href="../builtin/index.html#bool">bool</a></pre>
  726. <p>
  727. Match reports whether the Regexp matches the byte slice b.
  728. </p>
  729. <h3 id="Regexp.MatchReader">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=12661:12712#L395">MatchReader</a></h3>
  730. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) MatchReader(r <a href="../io/index.html">io</a>.<a href="../io/index.html#RuneReader">RuneReader</a>) <a href="../builtin/index.html#bool">bool</a></pre>
  731. <p>
  732. MatchReader reports whether the Regexp matches the text read by the
  733. RuneReader.
  734. </p>
  735. <h3 id="Regexp.MatchString">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=12828:12872#L400">MatchString</a></h3>
  736. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) MatchString(s <a href="../builtin/index.html#string">string</a>) <a href="../builtin/index.html#bool">bool</a></pre>
  737. <p>
  738. MatchString reports whether the Regexp matches the string s.
  739. </p>
  740. <h3 id="Regexp.NumSubexp">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=9164:9197#L246">NumSubexp</a></h3>
  741. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) NumSubexp() <a href="../builtin/index.html#int">int</a></pre>
  742. <p>
  743. NumSubexp returns the number of parenthesized subexpressions in this Regexp.
  744. </p>
  745. <h3 id="Regexp.ReplaceAll">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=17420:17473#L543">ReplaceAll</a></h3>
  746. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) ReplaceAll(src, repl []<a href="../builtin/index.html#byte">byte</a>) []<a href="../builtin/index.html#byte">byte</a></pre>
  747. <p>
  748. ReplaceAll returns a copy of src, replacing matches of the Regexp
  749. with the replacement text repl. Inside repl, $ signs are interpreted as
  750. in Expand, so for instance $1 represents the text of the first submatch.
  751. </p>
  752. <h3 id="Regexp.ReplaceAllFunc">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=18374:18451#L571">ReplaceAllFunc</a></h3>
  753. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) ReplaceAllFunc(src []<a href="../builtin/index.html#byte">byte</a>, repl func([]<a href="../builtin/index.html#byte">byte</a>) []<a href="../builtin/index.html#byte">byte</a>) []<a href="../builtin/index.html#byte">byte</a></pre>
  754. <p>
  755. ReplaceAllFunc returns a copy of src in which all matches of the
  756. Regexp have been replaced by the return value of function repl applied
  757. to the matched byte slice. The replacement returned by repl is substituted
  758. directly, without using Expand.
  759. </p>
  760. <h3 id="Regexp.ReplaceAllLiteral">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=17945:18005#L561">ReplaceAllLiteral</a></h3>
  761. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) ReplaceAllLiteral(src, repl []<a href="../builtin/index.html#byte">byte</a>) []<a href="../builtin/index.html#byte">byte</a></pre>
  762. <p>
  763. ReplaceAllLiteral returns a copy of src, replacing matches of the Regexp
  764. with the replacement bytes repl. The replacement repl is substituted directly,
  765. without using Expand.
  766. </p>
  767. <h3 id="Regexp.ReplaceAllLiteralString">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=14797:14863#L459">ReplaceAllLiteralString</a></h3>
  768. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) ReplaceAllLiteralString(src, repl <a href="../builtin/index.html#string">string</a>) <a href="../builtin/index.html#string">string</a></pre>
  769. <p>
  770. ReplaceAllLiteralString returns a copy of src, replacing matches of the Regexp
  771. with the replacement string repl. The replacement repl is substituted directly,
  772. without using Expand.
  773. </p>
  774. <div id="example_Regexp_ReplaceAllLiteralString" class="toggle">
  775. <div class="collapsed">
  776. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  777. </div>
  778. <div class="expanded">
  779. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  780. <p>Code:</p>
  781. <pre class="code">re := regexp.MustCompile(&#34;a(x*)b&#34;)
  782. fmt.Println(re.ReplaceAllLiteralString(&#34;-ab-axxb-&#34;, &#34;T&#34;))
  783. fmt.Println(re.ReplaceAllLiteralString(&#34;-ab-axxb-&#34;, &#34;$1&#34;))
  784. fmt.Println(re.ReplaceAllLiteralString(&#34;-ab-axxb-&#34;, &#34;${1}&#34;))
  785. <span class="comment"></pre>
  786. <p>Output:</p>
  787. <pre class="output">-T-T-
  788. -$1-$1-
  789. -${1}-${1}-
  790. </pre>
  791. </div>
  792. </div>
  793. <h3 id="Regexp.ReplaceAllString">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=14324:14383#L445">ReplaceAllString</a></h3>
  794. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) ReplaceAllString(src, repl <a href="../builtin/index.html#string">string</a>) <a href="../builtin/index.html#string">string</a></pre>
  795. <p>
  796. ReplaceAllString returns a copy of src, replacing matches of the Regexp
  797. with the replacement string repl. Inside repl, $ signs are interpreted as
  798. in Expand, so for instance $1 represents the text of the first submatch.
  799. </p>
  800. <div id="example_Regexp_ReplaceAllString" class="toggle">
  801. <div class="collapsed">
  802. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  803. </div>
  804. <div class="expanded">
  805. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  806. <p>Code:</p>
  807. <pre class="code">re := regexp.MustCompile(&#34;a(x*)b&#34;)
  808. fmt.Println(re.ReplaceAllString(&#34;-ab-axxb-&#34;, &#34;T&#34;))
  809. fmt.Println(re.ReplaceAllString(&#34;-ab-axxb-&#34;, &#34;$1&#34;))
  810. fmt.Println(re.ReplaceAllString(&#34;-ab-axxb-&#34;, &#34;$1W&#34;))
  811. fmt.Println(re.ReplaceAllString(&#34;-ab-axxb-&#34;, &#34;${1}W&#34;))
  812. <span class="comment"></pre>
  813. <p>Output:</p>
  814. <pre class="output">-T-T-
  815. --xx-
  816. ---
  817. -W-xxW-
  818. </pre>
  819. </div>
  820. </div>
  821. <h3 id="Regexp.ReplaceAllStringFunc">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=15246:15329#L469">ReplaceAllStringFunc</a></h3>
  822. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) ReplaceAllStringFunc(src <a href="../builtin/index.html#string">string</a>, repl func(<a href="../builtin/index.html#string">string</a>) <a href="../builtin/index.html#string">string</a>) <a href="../builtin/index.html#string">string</a></pre>
  823. <p>
  824. ReplaceAllStringFunc returns a copy of src in which all matches of the
  825. Regexp have been replaced by the return value of function repl applied
  826. to the matched substring. The replacement returned by repl is substituted
  827. directly, without using Expand.
  828. </p>
  829. <h3 id="Regexp.Split">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=33846:33895#L1093">Split</a></h3>
  830. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) Split(s <a href="../builtin/index.html#string">string</a>, n <a href="../builtin/index.html#int">int</a>) []<a href="../builtin/index.html#string">string</a></pre>
  831. <p>
  832. Split slices s into substrings separated by the expression and returns a slice of
  833. the substrings between those expression matches.
  834. </p>
  835. <p>
  836. The slice returned by this method consists of all the substrings of s
  837. not contained in the slice returned by FindAllString. When called on an expression
  838. that contains no metacharacters, it is equivalent to strings.SplitN.
  839. </p>
  840. <p>
  841. Example:
  842. </p>
  843. <pre>s := regexp.MustCompile(&#34;a*&#34;).Split(&#34;abaabaccadaaae&#34;, 5)
  844. // s: [&#34;&#34;, &#34;b&#34;, &#34;b&#34;, &#34;c&#34;, &#34;cadaaae&#34;]
  845. </pre>
  846. <p>
  847. The count determines the number of substrings to return:
  848. </p>
  849. <pre>n &gt; 0: at most n substrings; the last substring will be the unsplit remainder.
  850. n == 0: the result is nil (zero substrings)
  851. n &lt; 0: all substrings
  852. </pre>
  853. <div id="example_Regexp_Split" class="toggle">
  854. <div class="collapsed">
  855. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  856. </div>
  857. <div class="expanded">
  858. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  859. <p>Code:</p>
  860. <pre class="code">a := regexp.MustCompile(&#34;a&#34;)
  861. fmt.Println(a.Split(&#34;banana&#34;, -1))
  862. fmt.Println(a.Split(&#34;banana&#34;, 0))
  863. fmt.Println(a.Split(&#34;banana&#34;, 1))
  864. fmt.Println(a.Split(&#34;banana&#34;, 2))
  865. zp := regexp.MustCompile(&#34;z+&#34;)
  866. fmt.Println(zp.Split(&#34;pizza&#34;, -1))
  867. fmt.Println(zp.Split(&#34;pizza&#34;, 0))
  868. fmt.Println(zp.Split(&#34;pizza&#34;, 1))
  869. fmt.Println(zp.Split(&#34;pizza&#34;, 2))
  870. <span class="comment"></pre>
  871. <p>Output:</p>
  872. <pre class="output">[b n n ]
  873. []
  874. [banana]
  875. [b nana]
  876. [pi a]
  877. []
  878. [pizza]
  879. [pi a]
  880. </pre>
  881. </div>
  882. </div>
  883. <h3 id="Regexp.String">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=4104:4137#L93">String</a></h3>
  884. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) String() <a href="../builtin/index.html#string">string</a></pre>
  885. <p>
  886. String returns the source text used to compile the regular expression.
  887. </p>
  888. <h3 id="Regexp.SubexpNames">func (*Regexp) <a href="http://localhost:6060/src/regexp/regexp.go?s=9560:9600#L255">SubexpNames</a></h3>
  889. <pre>func (re *<a href="index.html#Regexp">Regexp</a>) SubexpNames() []<a href="../builtin/index.html#string">string</a></pre>
  890. <p>
  891. SubexpNames returns the names of the parenthesized subexpressions
  892. in this Regexp. The name for the first sub-expression is names[1],
  893. so that if m is a match slice, the name for m[i] is SubexpNames()[i].
  894. Since the Regexp as a whole cannot be named, names[0] is always
  895. the empty string. The slice should not be modified.
  896. </p>
  897. <div id="example_Regexp_SubexpNames" class="toggle">
  898. <div class="collapsed">
  899. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  900. </div>
  901. <div class="expanded">
  902. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  903. <p>Code:</p>
  904. <pre class="code">re := regexp.MustCompile(&#34;(?P&lt;first&gt;[a-zA-Z]+) (?P&lt;last&gt;[a-zA-Z]+)&#34;)
  905. fmt.Println(re.MatchString(&#34;Alan Turing&#34;))
  906. fmt.Printf(&#34;%q\n&#34;, re.SubexpNames())
  907. reversed := fmt.Sprintf(&#34;${%s} ${%s}&#34;, re.SubexpNames()[2], re.SubexpNames()[1])
  908. fmt.Println(reversed)
  909. fmt.Println(re.ReplaceAllString(&#34;Alan Turing&#34;, reversed))
  910. <span class="comment"></pre>
  911. <p>Output:</p>
  912. <pre class="output">true
  913. [&#34;&#34; &#34;first&#34; &#34;last&#34;]
  914. ${last} ${first}
  915. Turing Alan
  916. </pre>
  917. </div>
  918. </div>
  919. <h2 id="pkg-subdirectories">Subdirectories</h2>
  920. <div class="pkg-dir">
  921. <table>
  922. <tr>
  923. <th class="pkg-name">Name</th>
  924. <th class="pkg-synopsis">Synopsis</th>
  925. </tr>
  926. <tr>
  927. <td colspan="2"><a href="http://localhost:6060/pkg/">..</a></td>
  928. </tr>
  929. <tr>
  930. <td class="pkg-name" style="padding-left: 0px;">
  931. <a href="syntax/index.html">syntax</a>
  932. </td>
  933. <td class="pkg-synopsis">
  934. Package syntax parses regular expressions into parse trees and compiles parse trees into programs.
  935. </td>
  936. </tr>
  937. </table>
  938. </div>
  939. <div id="footer">
  940. Build version go1.6.<br>
  941. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  942. the content of this page is licensed under the
  943. Creative Commons Attribution 3.0 License,
  944. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  945. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  946. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  947. </div>
  948. </div><!-- .container -->
  949. </div><!-- #page -->
  950. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  951. <script type="text/javascript" src="../../lib/godoc/jquery.js"></script>
  952. <script type="text/javascript" src="../../lib/godoc/jquery.treeview.js"></script>
  953. <script type="text/javascript" src="../../lib/godoc/jquery.treeview.edit.js"></script>
  954. <script type="text/javascript" src="../../lib/godoc/godocs.js"></script>
  955. </body>
  956. </html>