Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by oliver-jones · Jul 11, 2019 at 04:05 PM · stringparsestring.split

Parse String and Serialise

Hello,

I have a body text that comes into my game, which I need to parse and serialise certain sections of it, due to it containing fields and values for a calculator. The text comes externally from a source, and I have no control over it. Let me show you the text body first;

 ---------------
 Hello, this is some text.
 Below you'll see a calculator
 
 [calculator
 field1="value1" field2="value2" field3="value3" field4="value4"
 /]
 
 Above is a calculator.
 ---------------


So firstly, I need to remove the [calculator ... /] (which is a static tag) section from the text. I then need to take that removed section and parse it. The format is always;

 field="value"

With only a space, or linebreak separating each entry.

I've been using string.Split, but it is very messy, and I'm pretty sure there are easier methods than this?


Thanks.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · Jul 11, 2019 at 05:33 PM 0
Share

If the format is known and consistent then consider regular expressions to parse the data.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by sarahnorthway · Jul 11, 2019 at 06:41 PM

Sounds like a job for the mighty Regular Expression! Regex patterns can get gnarly but regex101.com is a fantastic resource to help build and debug them.

I would pull out the field1="value1" field2="value2" string in a first step, then pull the pairs from that.

Like...

 using System.Text.RegularExpressions;
 using UnityEngine;
 
 [ExecuteInEditMode]
 public class test : MonoBehaviour {
     private static string text = " ---------------\r\n Hello, this is some text.\r\n Below you'll see a calculator\r\n \r\n [calculator\r\n field1=\"value1\" field2=\"value2\" field3=\"value3\" field4=\"value4\"\r\n /]\r\n \r\n Above is a calculator.\r\n ---------------";
     
     void OnEnable() {
         // look for single match of whatever's between [calculator and /]
         string pattern = @"\[calculator([^\]]*)\/\]";
         Match match = Regex.Match(text, pattern);
         if (string.IsNullOrEmpty(match.Value)) {
             Debug.LogWarning("Invalid calculator string.");
             return;
         }
 
         // field1="value1" field2="value2" field3="value3" field4="value4" 
         // Groups[0] is the entire match, Groups[1] is the 1st capturing group denoted by ()
         string pairs = match.Groups[1].Value.Trim();
         Debug.Log("Pairs: " + pairs);
         
         // look for multiple matches of XXXX="YYYY"
         // to escape " in an @ string replace with ""
         string pairsPattern = @"([^=]*)=""([^""]*)""";
         MatchCollection matches = Regex.Matches(pairs, pairsPattern);
         foreach (Match pairsMatch in matches) {
             string field = pairsMatch.Groups[1].Value.Trim();
             string value = pairsMatch.Groups[2].Value.Trim();
             Debug.Log(field + "=" + value);
         }
     }
 }
 

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oliver-jones · Jul 12, 2019 at 08:21 AM 0
Share

Hello @sarahnorthway , thank you very much for this. I have heard of Regex, and it does seem very useful indeed. Thank you for the Regex tester, I'm having a play now. Could you comment/explain a bit further? On line 19, why are you using Trim? Also I inserted the field=value into the Regex tester using your expression, and I get 'Your regular expression does not match the subject string.' -- Update -- I replaced your expression with this;

 ([^=]*)="([^""]*)

And it works on the tester

avatar image sarahnorthway oliver-jones · Jul 12, 2019 at 03:59 PM 0
Share

Yeah using "" to escape " always throws me. In the sim it should be ([^=]*)="([^"]*)" which means (anything NOT an equals sign)="(anything NOT a double quote)"

I always Trim to remove whitespace on unknown values just to be safe... but in this case the 2nd+ field name will end up with a space at the start if you don't. To account for it you could do string pairsPattern = @"\w*([^=]*)=""([^""]*)"""; which matches but ignores 0 or more whitespace characters (\w) from the start of each pair.

avatar image oliver-jones sarahnorthway · Jul 22, 2019 at 02:07 PM 0
Share

Thanks for this, one thing I forgot to mention is that there could be multiple calculators on the same text, loads in fact. Can the $$anonymous$$atch take this into account?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

110 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Highscores Input wrong Format Error 1 Answer

Getting text from a website file and storing it in a variable 1 Answer

What do you say the best way to store a string be? 2 Answers

String Into Seperate characters problem 1 Answer

Parsing String from script A into Enum in script B 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges