# Is there a way to achieve right-to-left behavior in Swift's new Regex?

**URL:** <https://forums.swift.org/t/is-there-a-way-to-achieve-right-to-left-behavior-in-swifts-new-regex/66094>\
**Category:** Using Swift\
**Tags:** regex\
**Created:** [July 11, 2023, 12:51am UTC](https://forums.swift.org/t/is-there-a-way-to-achieve-right-to-left-behavior-in-swifts-new-regex/66094 "2023-07-11T00:51:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Weskey](https://avatars.discourse-cdn.com/v4/letter/w/ecb155/32.png) [@Weskey](https://forums.swift.org/u/Weskey)\
**Post date:** [July 11, 2023, 12:51am UTC](https://forums.swift.org/t/is-there-a-way-to-achieve-right-to-left-behavior-in-swifts-new-regex/66094/1 "2023-07-11T00:51:46Z")

</div>

Hello Swift Community,

As a newbie to Swift, I've been exploring the new Regex capabilities introduced last year. I have a question regarding the right-to-left behavior of regular expressions, similar to the [RegexOptions.RightToLeft](https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-options#right-to-left-mode) option in C#:

> By default, the regular expression engine searches from left to right. You can reverse the search direction by using the [RegexOptions.RightToLeft](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regexoptions#system-text-regularexpressions-regexoptions-righttoleft) option. The right-to-left search automatically begins at the last character position of the string. For pattern-matching methods that include a starting position parameter, such as [Regex.Match(String, Int32)](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.match#system-text-regularexpressions-regex-match(system-string-system-int32)), the specified starting position is the index of the rightmost character position at which the search is to begin.

This behavior is particularly useful when matching patterns from the end of the string or when using starting position parameters.

However, I'm having trouble finding an equivalent approach in Swift's new Regex. The `firstMatch(of:)` method seems to match from left to right by default, and I haven't come across a direct option to change the search direction.

For example:

```swift
let regexYearDigit = try! Regex("\\d{4}").dotMatchesNewlines().dotMatchesNewlines()
let name = "Test (2021) - January 2019.pdf"
if let yearMatches = name.firstMatch(of: regexYearDigit){
    print(yearMatches.0)
    let updatedName = name.replacing(regexYearDigit, with: "2023")
}

```

I would like to get the 2019 value instead 2021 and change by 2023.

Thank you in advance for your guidance and assistance!

Best regards,

---

<div class="post-metadata">

**Author:** ![ksemianov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ksemianov/32/18723_2.png) [@ksemianov](https://forums.swift.org/u/ksemianov)\
**Post date:** [July 11, 2023, 9:57am UTC](https://forums.swift.org/t/is-there-a-way-to-achieve-right-to-left-behavior-in-swifts-new-regex/66094/2 "2023-07-11T09:57:51Z")

</div>

How about `name.matches(of: regexYearDigit).last` instead of `name.firstMatch(of: regexYearDigit)`?

---

<div class="post-metadata">

**Author:** ![DandyLyons](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dandylyons/32/14417_2.png) [@DandyLyons](https://forums.swift.org/u/DandyLyons)\
**Post date:** [September 19, 2024, 2:24am UTC](https://forums.swift.org/t/is-there-a-way-to-achieve-right-to-left-behavior-in-swifts-new-regex/66094/3 "2024-09-19T02:24:16Z")

</div>

Looks like Backward algorithms was mentioned as a future direction for the proposal.

> <https://github.com/swiftlang/swift-evolution/blob/main/proposals/0357-regex-string-processing-algorithms.md#backward-algorithms>

But there does appear to be a pitch in progress to add it.

> [@\[Pitch\] Regex Reverse Matching](https://forums.swift.org/t/pitch-regex-reverse-matching/73482):
>
> Regex reverse matching Proposal: SE-NNNN Authors: [Jacob Hearst](https://github.com/JacobHearst) [Michael Ilseman](https://github.com/milseman) Review Manager: TBD Status: Awaiting implementation Bug: rdar://132158055 Implementation: [Prototype](https://github.com/JacobHearst/swift-experimental-string-processing/tree/reverse-matching) Upcoming Feature Flag: (pending) Introduction Regex supports matching strings forwards, including lookahead assertions, but does not currently support matching in reverse or lookbehind assertions. We propose adding these. Motivation Modern regular expression engines support lookbehind assertions, whether fixed length…

---

<div class="post-metadata">

**Author:** ![JacobHearst](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jacobhearst/32/31923_2.png) [@JacobHearst](https://forums.swift.org/u/JacobHearst)\
**Post date:** [September 19, 2024, 12:09pm UTC](https://forums.swift.org/t/is-there-a-way-to-achieve-right-to-left-behavior-in-swifts-new-regex/66094/4 "2024-09-19T12:09:49Z")

</div>

Unfortunately reverse matching was cut from the proposal so the pitch is now purely for lookbehind support.
