Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Regex to find tag in string

Discussion in 'Scripting' started by Burton-kun, Apr 7, 2020.

  1. Burton-kun

    Burton-kun

    Joined:
    Aug 6, 2015
    Posts:
    50
    Pretty much what the title says. I have a string with some tags for rich text. I'm looking for those tags and contents too with a regular expression. Following, the code till this moment. Just a function and a regex, I was still debugging.

    Code (csharp):
    1.  
    2. public void GetTags(string message)
    3. {
    4.     Match match = regex.Match(message);
    5.  
    6.     foreach (Group g in match.Groups)
    7.     {
    8.         Debug.Log(g.Value);
    9.     }
    10. }
    11.  
    12. public Regex regex = new Regex(@"(?<start_tag><[^>]*>)(?<tag>.*?)(?<end_tag></[^>]*>)");
    13.  
    I tested the regular expression and seems to work well, but when I try it in Unity nothing happens. Sometimes I get empty message in console, other times I get nothing at all. The groups seems to be inexistent. What am I doing wrong?
     
    Last edited: Apr 8, 2020
  2. Burton-kun

    Burton-kun

    Joined:
    Aug 6, 2015
    Posts:
    50
    anybody knows?