Search Unity

Regex multiple patterns and replacements?

Discussion in 'Scripting' started by L_A_R, Jan 19, 2020.

  1. L_A_R

    L_A_R

    Joined:
    May 14, 2017
    Posts:
    34
    Problem:
    - I need to used Regex to look for multiple patterns and replace them with individual replacements.
    - I think it's possible to use a list or dictionary to do it but I don't know how and all my googling hasn't landed me in the right spot.

    Example code of current inefficient solution:

    Code (CSharp):
    1. using System;
    2. using System.Text.RegularExpressions;
    3.  
    4. public class Class1
    5. {
    6.  
    7.     string result1;
    8.     string result2;
    9.     string input;
    10.     string replacement;
    11.     int replacementNum;
    12.     int replacementNumTwo;
    13.     int SmallNumber;
    14.     int SmallNumberTwo;
    15.     string[] Alphabet = new string[3] { "a", "b", "c" };
    16.     string[] AlphabetTwo = new string[5] { "d", "e", "x", "y", "z" };
    17.     string Letter;
    18.     string LetterTwo;
    19.  
    20.  
    21.     void Start() //START RUNS AND SETS UP THE VARIABLES
    22.     {
    23.  
    24.         SetupVariables();
    25.     }
    26.  
    27.     void SetupVariables() //1.USES Random.Range TO DEFINE OUR VARIABLES
    28.     {
    29.         input = "SmallNumber Letter SmallNumberTwo LetterTwo";
    30.         Letter = Alphabet[Random.Range(0, Alphabet.Length)];
    31.         LetterTwo = AlphabetTwo[Random.Range(0, AlphabetTwo.Length)];
    32.         SmallNumber = Random.Range(2, 5);
    33.         SmallNumberTwo = Random.Range(2, 7);
    34.         ReplaceNumber();
    35.     }
    36.  
    37.     void ReplaceNumber() //2.REGEX IS THEN USED TO SEARCH FOR PATTERNS, REPLACING MATCHES WITH OUR VARIABLES
    38.     {
    39.         //first number
    40.        
    41.         Debug.Log("input 1: " + input);
    42.         pattern = @"smallNumber(\w*)";
    43.         replacementNum = SmallNumber;
    44.         Regex rgx = new Regex(pattern);
    45.         result1 = rgx.Replace(input, replacementNum.ToString());
    46.         ReplaceLetter();
    47.     }
    48.  
    49.     void ReplaceLetter() //3.REGEX IS THEN USED ON THE OUTPUT OF THIS TO DO THE SAME AGAIN
    50.     {
    51.         //letter replacement
    52.         input = result1;
    53.         pattern = @"Letter(\w*)";
    54.         replacement = Letter;
    55.         Regex rgx = new Regex(pattern);
    56.         result3 = rgx.Replace(input, replacement);
    57.         ReplaceLetterTwo();
    58.     }
    59.  
    60.     //4.AND SO ON, ONCE ALL MATCHES DESIRED HAVE BEEN PERFORMED THE END RESULT IS LEFT IN A STRING
    61. }
    Desired help:
    Instead of using multiple functions and having one execute the next, how can I simply run multiple patterns and replacements in a much more efficient manner/eliminating the use of this ugly solution?

    Thank you for any help
     
  2. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    Hi, so the input string is like a pattern to generate strings like 3a5x?

    Why do you need regular expressions for that? I would do something like:
    input="#1#2#3#4";
    input=input.Replace("#1",Letter).Replace("#2",SmallNumber).Replace....

    I don't know if it is the most efficient way but if you don't do it every frame I wouldn't bother. Finding a regular expression to replace everything at once...I don't know if that is possible since no Replace() method is taking an infinite number of strings as arguments.