Hello, I'm trying to read my custom made syntax from a text file.
For example
Okay from that line all I'm trying to read is "01" and "C:\text.txt"Code:<#01 "C:\text.txt">
Any ideas?
Thank you in advance.
Printable View
Hello, I'm trying to read my custom made syntax from a text file.
For example
Okay from that line all I'm trying to read is "01" and "C:\text.txt"Code:<#01 "C:\text.txt">
Any ideas?
Thank you in advance.
If you made this syntax, why didn't you make it so that you knew how to read it? Seems a bit strange to create a custom syntax while you don't know how to read it back.
Anyway, you'd probably want to use regular expressions.
I'm very sorry NickThissen i should of given a more clear explanation.
This so called custom syntax which i created is nothing more then custom text which will be stored in text document.
Is it possible for me to read that line inside of a text document and only retract does specific values.
Quote:
Originally Posted by Pc_Not_Mac
Yes, I understood that. But my question was: why do you store the data in that particular way, when you know you don't know how to retrieve it back? That makes no sense. There are many cases where you don't have control over the syntax of your data (either it is coming from some third party, or some third party needs to read it), in which case you must comply with it, but in this case you can determine how to store the data, so if I were you I would store the data in a way that makes it easy for yourself to read it.
For example, I'm sure you know the String.Split function. Then why not just store the data as
and split it along the | character.Code:01|"C:\text.txt"
If you know how to read/write XML, then why not use that:
It just seems silly to create a custom syntax when you don't know how to read it. That's almost like creating your own language, but not knowing how to speak it.Code:<value id=1>C:\text.txt</value>
Anyway, if you insist to use this format, as I said regular expressions are probably easiest. I'm not very good at them, but my first try would be this pattern:
That's probably wrong though.Code:<#(d{1,4}) "(.*)">
I'll break it up for you to explain:
The <# part simply matches those characters. The d{1,4} part should match any number 1 to 4 digits (I think). I choose 4 randomly. The parenthesis around it indicate that this is a capture group, so that you can retrieve what's inside them (the number) later. The .* is inside another capture group and it means 'any amount of any character' basically. The dot matches any character and the asterisk indicates indefinite repetition.Code:<# ( d{1,4} ) " (.*) ">
To use this in code would be something like
Completely untested and from memory, but it should be close. The Groups in the Match object include the capture groups (anything between parenthesis in the pattern), but if I remember correctly the first group always contains the entire matched string. So the second group (index 1) contains the first capture group ("01" in your example), while the third group contains the text ("C:\test.txt" in your example).Code:Dim pattern = "<#(d{1,4}) ""(.*)"">" 'note use of double " chars inside the string
For Each m As Match In Regex.Match(sourceText, pattern)
Dim entireMatch = m.Groups(0)
Dim number = m.Groups(1)
Dim text = m.Groups(2)
Next
Thank you for the reply Nick.
I will use a database for my project and avoid using custom syntax.
But, i was just wondering how can one read this line in XML.
<value id=1>C:\text.txt</value>
Pretty good Regex from memory Nick! only a few things I'd change up, a "digit" is first preceded by a "\" so it becomes \d, also, I'd use a lookback for the <#
Something like this:
But as Nick said earlier, if you have the freedom to create your own save format, make an easier one. This one still needs to be split about the space (unless you want to use two regular expressions per line...) So you may as well put a single delimiter as Nick suggested and split about that and call the appropriate index.Code:Dim RetrievePattern As String = "(?<=<#)\d+ .+(?=>)"
Dim reg() As String = Regex.Match(TextBox1.Text, RetrievePattern).Value.Split(" "c)
MsgBox(reg(0))
MsgBox(reg(1))