Question: I'm new to f# and want to use it to alter the format of a propositional logic formula string:

I want to replace "aX" by the string "next(a)", with 'a' being an element of [a..z] and 'X' being the capital X' character.

All sources i found, e.g. <https://www.dotnetperls.com/replace-fs> either replace a string by another string,

```
let s = "a & b & c & aX & !bX"
let sReplaced = s.Replace("X", "next()") // val it : string = "a & b & c & anext() & !bnext()"

```

in which case you can not put the original character in between or if they work characterwise, as eg.

```
let sArray = s.ToCharArray()

for c in 0 .. sArray.Length - 1 do
    if sArray.[c] = 'X' then
        sArray.[c-2] <- '('
        sArray.[c] <- ')'
let sArrayResult = new string(sArray) // val sArrayResult : string = "a & b & c &(a) & (b)"

```

only allow the same length for the output string. 

"a & b & c & aX & !bX" 

should be replaced with 

"a & b & c & next(a) & !next(b)"

Is there some convinient way to handle this problem? Thanks in advance.

Answer: 
Apologies for necro-posting on an old thread with an accepted answer but using regex has a certain overhead, and a naïve iterative solution using lists might compare favourably in terms of performance:

```
let strXToNext (s : string) =
    let next c a =
        ')' :: c :: '(' :: 't' :: 'x' :: 'e' :: 'n' :: a
    let rec replX a = function
        | [] -> List.rev a
        | c :: 'X' :: cs when c >= 'a' && c <= 'z' ->
            replX (next c a) cs
        | c :: cs -> replX (c :: a) cs
    s |> List.ofSeq
      |> replX []
      |> List.toArray
      |> fun a -> Core.string a

```
[Regex.Replace](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.replace?view=netframework-4.5.2#System_Text_RegularExpressions_Regex_Replace_System_String_System_String_System_Text_RegularExpressions_MatchEvaluator_) is your friend:

```
open System.Text.RegularExpressions
let myReplace s =
    Regex.Replace (s, ".X", fun mat -> sprintf "next(%c)" <| mat.ToString().[0])

```

You can change the `.` to `[a-z]` or whatever pattern that matches what you call *a arbitrary character*.