0
Text.text = "<color=#00ff00>Green</color>"

enter image description here

It's looks find , but when get string it's also is "<color=#00ff00>Green</color>" , not "Green", I can't easy to detect "syntax error".

because if I can't find any tag in rich text. It's mean my syntax is right.

Cardstdani
  • 4,999
  • 3
  • 12
  • 31
TimChang
  • 2,249
  • 13
  • 25
  • Sorry in advance If I'm not getting your problem. Because of English. I guess that you are using Unity's default ```UnityEngine.UI.Text```. With some kind of rich text, and you are facing a problem because when you try to read a value you get the everything including rich text tag. let me know. I am right or wrong – Ghost The Punisher Jul 31 '20 at 10:56
  • 1
    And why not doing something like `Text.color = Color.green; Text.text = "Green";` ? – derHugo Jul 31 '20 at 11:44
  • @Ankit I read a vlaue well , but I want to show warning when my syntax error . for example "" it wrong syntax in rich text . so I want to show warning. – TimChang Aug 03 '20 at 01:16
  • @derHugo I really need to use rich text , My chars not all color same. – TimChang Aug 03 '20 at 01:18
  • @derHugo I guess that this problem can be solved using ```RAGEX``` aka ```Regular Expression```. What is your thoughts? – Ghost The Punisher Aug 03 '20 at 07:08
  • In general you could ofcourse construct some regex to check this. Or checkout [this post](https://stackoverflow.com/questions/26531086/c-sharp-regex-check-string-contains-html) for checking for valid html in general – derHugo Aug 03 '20 at 07:14
  • I would like to agree with you on this, but I think that if we are using a library that validates HTML, and `````` is not a HTML tag. so this ```lib``` might not pass this tag and throw/return ```false```. but we can't say anything without testing it. – Ghost The Punisher Aug 03 '20 at 07:22
  • Well you could ofcourse use e.g. `((.+)<\/color>)` as a pattern like `Regex.Matches(inputString, "((.+)<\/color>)");` This would return three match groups: `match.Groups[0].Value` the entire match, `match.Groups[1].Value` only the color e.g. `"#aa00ff"` and `match.Groups[2].Value` the text between the tags. So this at least allows you to get the text(s) between such tags. However this would still not be enough to use it in order to validate the input ... – derHugo Aug 03 '20 at 07:37

1 Answers1

1

At least for getting the result you can use

You can probably use Regex.Matches and Match.Groups like e.g.

const string pattern = "(<color=(\"#[a-f0-9]{6}\"|#\"[a-f0-9]{4}\")>(.+)<\/color>)";

string input = "<color=\"#00ff00\">Green<\/color>";
var matches = Regex.Matches(input, pattern);

var stringBuilder = new StringBuilder();

foreach(var match in matches)
{
    var groups = match.Groups;
   
    // This will hold the entire matchg group "<color=\"#00ff00\">Green<\/color>"
    var entireMatch = groups[0].Value;
    // This will hold the color value like "\"#00ffaa\""
    var color = groups[1].Value;
    // This will hold the text like "Green"
    var text = groups[2].Value;

    rawText.Append(text);
}

var rawText = stringBuilder.ToString();

This would not be enough to actually validate the input, though.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • thanks Regex look more easy to detect for me , I will try it. – TimChang Aug 03 '20 at 10:05
  • 1
    yeah the problem is o course that this tries to find matches ... so if someone writes `ccolor` it wouldn't be a match and simply disappear ;) – derHugo Aug 03 '20 at 10:48