Arma 3 Scripting

Arma 3: Regular Expressions

Topic · Markdown · JSON · original wiki page

Arma 3 v2.06

A Regular Expression (or regex/regexp) is an advanced text search format involving specific codes.

Note

  • For a detailed explanation of what a Regular Expression (Regex) is, please visit the Wikipedia page about it.
  • The following pages allow to test regular expressions:
    • https://regexr.com/
    • https://regex101.com/

Quick Guide

Note

To match a specific character that is used in regex syntax, escape it with \, e.g I\.\.\. don't know\?

Commands

See Command Group: Strings - Regular Expression.

Flags

In order to adjust the behaviour of the regex commands, certain flags can be set when using them. Flags are specified at the end of the pattern and start with /. Flags need to be lowercase. If there are any non-flag characters in the flags they will be ignored and considered part of the pattern itself.

Note

If no flags are specified, the default flags are set to g and i. It is valid to specify / to indicate no flags, first match only, case sensitive - see Examples.

Flag Name Description
g Global Only relevant for regexReplace and regexFind.
Missing the global flag sets format_first_only flag (source) and:
- only replaces the first occurrence with regexReplace
- only returns the first element with regexFind
i Case-insensitive N/A
n noSubs

« When performing matches, all marked sub-expressions (expr) are treated as non-marking sub-expressions (?:expr). No matches are stored in the supplied std::regex_match structure and mark_count() is zero » – c++ reference (source)

o Optimise Optimise pattern, pattern creation is slower, but will execute more efficiently (source)

Examples

"1, 42, and 10e10" regexFind ["[0-9]"]; // matches "1", "4", "2", "1", "0" "1" and "0"
"1, 42, and 10e10" regexFind ["[0-9]+"]; // matches "1", "42", "10" and "10", avoiding commas and spaces
"Hello there!" regexMatch "There";        // matches - default flags g and i are active
"Hello there!" regexMatch "There/";        // no flags are set - as "There" is different from "there" and search is case-sensitive, so the match fails
"Hello there!" regexMatch "There/i";    // matches - only the i flag is active
"I like garlic, onions and cheese" regexFind ["([^ ]+)(?:,| and) "]; // matches "garlic" and "onions"
"Existing Arma: Arma 0, ArmA, ArmA 1, Arma 1, Arma 2, Arma 3, Arma 4, Arma 5, Arma 2035" regexFind ["(Arma [0-3])[^0-9]/g"]; // matches "Arma 0", "Arma 1", "Arma 2", "Arma 3"