# Regex
<span class="source-link">[[Source]](src/regex/regex.md#L50)</span>

A perl compatible regular expression. This uses the PCRE2 library, and
attempts to enable JIT matching whenever possible.


```pony
class ref Regex
```

## Constructors

### create
<span class="source-link">[[Source]](src/regex/regex.md#L58)</span>


Compile a regular expression. Raises an error for an invalid expression.


```pony
new ref create(
  from: (String box | Array[U8 val] box),
  jit: Bool val = seq)
: Regex ref^ ?
```
#### Parameters

*   from: ([String](builtin-String) box | [Array](builtin-Array)\[[U8](builtin-U8) val\] box)
*   jit: [Bool](builtin-Bool) val = seq

#### Returns

* [Regex](regex-Regex) ref^ ?

---

## Public Functions

### matches
<span class="source-link">[[Source]](src/regex/regex.md#L76)</span>


Creates a match iterator from the regular expression that will iterate
over the supplied subject returning matches.


```pony
fun box matches(
  subject: String val)
: MatchIterator ref
```
#### Parameters

*   subject: [String](builtin-String) val

#### Returns

* [MatchIterator](regex-MatchIterator) ref

---

### eq
<span class="source-link">[[Source]](src/regex/regex.md#L83)</span>


Return true on a successful match, false otherwise.


```pony
fun box eq(
  subject: (String box | Array[U8 val] box))
: Bool val
```
#### Parameters

*   subject: ([String](builtin-String) box | [Array](builtin-Array)\[[U8](builtin-U8) val\] box)

#### Returns

* [Bool](builtin-Bool) val

---

### ne
<span class="source-link">[[Source]](src/regex/regex.md#L95)</span>


Return false on a successful match, true otherwise.


```pony
fun box ne(
  subject: (String box | Array[U8 val] box))
: Bool val
```
#### Parameters

*   subject: ([String](builtin-String) box | [Array](builtin-Array)\[[U8](builtin-U8) val\] box)

#### Returns

* [Bool](builtin-Bool) val

---

### apply
<span class="source-link">[[Source]](src/regex/regex.md#L101)</span>


Match the supplied string, starting at the given offset. Returns a Match
object that can give precise match details. Raises an error if there is no
match.


```pony
fun box apply(
  subject: (String val | Array[U8 val] val),
  offset: USize val = seq)
: Match ref^ ?
```
#### Parameters

*   subject: ([String](builtin-String) val | [Array](builtin-Array)\[[U8](builtin-U8) val\] val)
*   offset: [USize](builtin-USize) val = seq

#### Returns

* [Match](regex-Match) ref^ ?

---

### replace\[optional A: ([Seq](builtin-Seq)\[[U8](builtin-U8) val\] iso & ([String](builtin-String) iso | [Array](builtin-Array)\[[U8](builtin-U8) val\] iso))\]
<span class="source-link">[[Source]](src/regex/regex.md#L110)</span>


Perform a match on the subject, starting at the given offset, and create
a new string using the value as a replacement for what was matched. Raise
an error if there is no match.


```pony
fun box replace[optional A: (Seq[U8 val] iso & (String iso | Array[U8 val] iso))](
  subject: (String val | Array[U8 val] val),
  value: (String box | Array[U8 val] box),
  offset: USize val = seq,
  global: Bool val = seq)
: A^ ?
```
#### Parameters

*   subject: ([String](builtin-String) val | [Array](builtin-Array)\[[U8](builtin-U8) val\] val)
*   value: ([String](builtin-String) box | [Array](builtin-Array)\[[U8](builtin-U8) val\] box)
*   offset: [USize](builtin-USize) val = seq
*   global: [Bool](builtin-Bool) val = seq

#### Returns

* A^ ?

---

### split
<span class="source-link">[[Source]](src/regex/regex.md#L158)</span>


Split subject by non-empty occurrences of this pattern, returning a list
of the substrings.


```pony
fun box split(
  subject: String val,
  offset: USize val = seq)
: Array[String val] iso^ ?
```
#### Parameters

*   subject: [String](builtin-String) val
*   offset: [USize](builtin-USize) val = seq

#### Returns

* [Array](builtin-Array)\[[String](builtin-String) val\] iso^ ?

---

### index
<span class="source-link">[[Source]](src/regex/regex.md#L184)</span>


Returns the index of a named capture. Raises an error if the named capture
does not exist.


```pony
fun box index(
  name: String box)
: USize val ?
```
#### Parameters

*   name: [String](builtin-String) box

#### Returns

* [USize](builtin-USize) val ?

---

### dispose
<span class="source-link">[[Source]](src/regex/regex.md#L198)</span>


Free the underlying PCRE2 data.


```pony
fun ref dispose()
: None val
```

#### Returns

* [None](builtin-None) val

---

## Private Functions

### _match
<span class="source-link">[[Source]](src/regex/regex.md#L207)</span>


Match the subject and keep the capture results. Raises an error if there
is no match.


```pony
fun box _match(
  subject: (String box | Array[U8 val] box),
  offset: USize val,
  options: U32 val)
: Pointer[_Match val] ref ?
```
#### Parameters

*   subject: ([String](builtin-String) box | [Array](builtin-Array)\[[U8](builtin-U8) val\] box)
*   offset: [USize](builtin-USize) val
*   options: [U32](builtin-U32) val

#### Returns

* [Pointer](builtin-Pointer)\[[_Match](regex-_Match) val\] ref ?

---

### _final
<span class="source-link">[[Source]](src/regex/regex.md#L236)</span>


Free the underlying PCRE2 data.


```pony
fun box _final()
: None val
```

#### Returns

* [None](builtin-None) val

---

