Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Jacce · Sep 28, 2013 at 06:23 PM · c#hashtableregex

Replace with a value from hashtable

Is it possible to do replace with a value from a hashtable when using Regex replace, depending on the string that Regex finds? I don't really know how to explain it good, so here's my code: string data_location; static Hashtable main;

 void Start ()
 {
     // Start with importing language files
     main = new Hashtable ();
     data_location = Application.dataPath + "/data/language/EN/";
     
     StreamReader main_file = new StreamReader (data_location + "main.txt");
     string main_language = main_file.ReadToEnd ();
     string[] main_language_lines = main_language.Split ('\n');
     
     foreach (string line in main_language_lines)
     {
         // Save every line
         if (line != "")
         {
             // Add the strings to the hashtable after processing them
             string pattern = "{([A-z0-9_]+)}";
             
             string[] line_content = line.Split ('|');
             main.Add (line_content[0], Regex.Replace (line_content[1].Replace ("\\n", "\n"), pattern, (string) main["$1"]));
         }
     }
 }

And here's the content of the file being imported:

 GAME_NAME|Derpflerpia
 VERSION|{GAME_NAME} PreAlpha v0.1
 WELCOME|Welcome to {GAME_NAME}!
 WELCOME_TEXT|{GAME_NAME} is still a WIP. Expect many derps and stoff.\n\nControls:\nW - Move forward\nA - Move left\nS - Move backwards\nD - Move right\nQ - Show current quests\nE - Teleport to the cave\nI - Open inventory\nP - Pause\nEsc - Close game\nEnter - Close this window
 BACKPACK|Backpack
 PAUSED|Paused
 QUESTS|Quests

The problem lies in this line: main.Add (line_content[0], Regex.Replace (line_content[1].Replace ("\\n", "\n"), pattern, (string) main["$1"]));

If I replace it to this it works: main.Add (line_content[0], Regex.Replace (line_content[1].Replace ("\\n", "\n"), pattern, (string) main["GAME_NAME"]));

But then it will not work in future cases with other strings, so any suggestions how I should do?

Sorry about bad explanations

Comment
Add comment
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

1 Reply

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

Answer by ArkaneX · Sep 28, 2013 at 08:01 PM

I'll start from different problem: you create StreamReader without closing or disposing it. You should either call Close/Dispose, or just use using keyword to automatically handle disposing

 using(StreamReader sr = new StreamReader(path))
 {
     // read the data
 }

You can use File.ReadAllLines as well, which has an advantage over StreamReader.ReadToEnd, because it returns array of lines.

As to your question: it doesn't work, because substitution ($1 here) must be passed directly to the Replace method. When you do main["$1"], substitution is not used, and you're just trying to get a value with key "$1" from hashtable. I suggest the following solution:

 Dictionary<string, string> main = new Dictionary<string, string>(); // I suggest using Dictionary, because it is strongly typed and doesn't require any casting to string, like you did in your code
 
 string pattern = @"\{([A-z0-9_]+)\}"; // this should be outside of foreach loop
 foreach (var line in main_language_lines)
 {
     string[] line_content = line.Split('|');
 
     string key = line_content[0];
     string value = line_content[1].Replace("\\n", "\n");
     var match = Regex.Match(value, pattern);
     if (match.Success)
     {
         value = Regex.Replace(value, pattern, main[match.Groups[1].Value]);
     }
     main.Add(key, value);
 }

Please note, that this is solution for single replacement. If you had the following line:

 {GAME_NAME}, version {VERSION}

then you need to modify the sample code.

Comment
Add comment · 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

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

16 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# multiple key/value pairs of various types in a single line of code...? 1 Answer

C# Multidimensional Collection to Store and Retrieve Character Information 1 Answer

Flip over an object (smooth transition) 3 Answers


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