v50 Steam/Premium information for editors
  • v50 information can now be added to pages in the main namespace. v0.47 information can still be found in the DF2014 namespace. See here for more details on the new versioning policy.
  • Use this page to report any issues related to the migration.
This notice may be cached—the current version can be found here.

Difference between revisions of "Template:Str/count/doc"

From Dwarf Fortress Wiki
Jump to navigation Jump to search
(Examples)
(→‎Examples: Another example with explanation)
Line 15: Line 15:
 
<pre>{{str/count|\w+|this is an example}}</pre>
 
<pre>{{str/count|\w+|this is an example}}</pre>
 
&rarr; {{str/count|\w+|this is an example}} (<code>\w+</code> matches one or more "word" characters, but not spaces)
 
&rarr; {{str/count|\w+|this is an example}} (<code>\w+</code> matches one or more "word" characters, but not spaces)
 +
 +
<pre>  {{str/count|\[\[[^]]+\]\]|This is text with a [[link]] and [[another link]].}}</pre>
 +
&rarr; {{str/count|\[\[[^]]+\]\]|This is text with a [[link]] and [[another link]].}}
 +
 +
Breakdown of regular expression:
 +
{| class="wikitable"
 +
! Section || Meaning || Explanation
 +
|-
 +
| <code>\[\[</code>
 +
| <code>[[</code>
 +
| Two ''literal'' <code>[</code> characters (note that <code>[</code> and <code>]</code> have a special meaning in regular expressions).
 +
|-
 +
| <code>[^]]</code>
 +
| Any character that is not <code>]</code>
 +
| A character class -- matches all characters listed between the brackets. The <code>^</code> is the negation operator, causing all characters ''not'' listed to be matched instead. In this case, the first <code>]</code> is the only character matched, since the second is the closing bracket. The first bracket is not treated as the closing bracket, since an empty character class is invalid.
 +
|-
 +
| <code>+</code>
 +
| Allows one or more matches of the previous expression
 +
| (in this case, it allows the non-<code>]</code> characters to be repeated)
 +
|-
 +
| <code>\]\]</code>
 +
| <code>]]</code>
 +
| Matches two literal <code>]</code> characters.
 +
|}

Revision as of 00:57, 25 June 2013

Essentially {{str/len}}, but counts the number of times {{{1}}} appears in {{{2}}}.

Note that {{{1}}} can be any regular expression.

Examples

{{str/count|a|abca}}

→ 2

{{str/count|\d|abc 123}}

→ 3

{{str/count|\D|abc 123}}

→ 4

{{str/count|\w+|this is an example}}

→ 4 (\w+ matches one or more "word" characters, but not spaces)

  {{str/count|\[\[[^]]+\]\]|This is text with a [[link]] and [[another link]].}}

→ 2

Breakdown of regular expression:

Section Meaning Explanation
\[\[ [[ Two literal [ characters (note that [ and ] have a special meaning in regular expressions).
[^]] Any character that is not ] A character class -- matches all characters listed between the brackets. The ^ is the negation operator, causing all characters not listed to be matched instead. In this case, the first ] is the only character matched, since the second is the closing bracket. The first bracket is not treated as the closing bracket, since an empty character class is invalid.
+ Allows one or more matches of the previous expression (in this case, it allows the non-] characters to be repeated)
\]\] ]] Matches two literal ] characters.