How Can I Parse Non-Uniform Substrings in VBA Excel?

In summary, the person is trying to figure out a way to parse a giant string of text that includes substrings that start and end with "<" and "<". They are looking for ideas on how to do this.
  • #1
Saladsamurai
3,020
7
This is VBA Excel:

Here is what I am trying to do. I have a MathML file saved as a .txt file. It is simialr to XML.

From the XML file, we have a bunch of text that looks something like:

<mname>Salad</mname><mrow>xyz</mrow>

I would ultimately like to have an array whose elements are the substrings:

Array(1) = <mname>
Array(2) = Salad
Array(3) = </mname>
...So far what I have done is this

Load the entire text file into one continuous string.

I have then fed the string into an array called ElementaryArray such that each individual character of the giant string is an element of the array.

Now I would like to sweep through the array, element by element, and determine the start and end positions of each substring.

If it were simply a bunch of substrings like <mmm><mmm><rrr><rrr><ooo><ooo> it would be easy enough. I could simply find the first "<" and then find its corresponding closing ">" and then restart the loop at the ">" position.

The problem is that not all of the strings start and end with the "<" & ">" characters.

I need a way to determine if the character after a ">" is another "<" or not. And then if it is not another "<" I must mark that character's position and then find the next occurrence of "<" which will be the end position (+1) of the substring that does not start with a "<".Does that all make sense :smile: The tricky part is relating the two different cases via the counter such that I do not get any overlap.

Any ideas?
 
Technology news on Phys.org
  • #2
I am of course also open to suggestions of alternative approaches to this problem.
 
  • #3
Don't try to hack up a parser by yourself. Use an XML parser library (MathML is XML).
 
  • #4
mXSCNT said:
Don't try to hack up a parser by yourself. Use an XML parser library (MathML is XML).

Is that your suggestion? Because that was not what I had in mind when I said I was open to suggestions. :wink:

Also, what does that mean?
 

FAQ: How Can I Parse Non-Uniform Substrings in VBA Excel?

What is a program's logic?

A program's logic is the underlying reasoning and decision-making process that guides how the program operates. It includes the sequence of steps and conditions that determine how the program will respond to different inputs and scenarios.

Why is it important to have a clear logic in a program?

A clear logic in a program is important for several reasons. It helps to ensure that the program will function correctly and produce the intended results. It also makes the program easier to understand and maintain, as well as allowing for easier troubleshooting and debugging if issues arise.

How can I improve the logic of my program?

There are several ways to improve the logic of a program. One way is to break down the problem into smaller, more manageable steps. Another is to use pseudocode or flowcharts to plan out the logic before writing the actual code. Additionally, getting feedback from others and actively testing and refining the program can also help improve its logic.

What are some common errors in program logic?

Some common errors in program logic include incorrect use of conditional statements, incorrect order or sequence of steps, and not accounting for all possible scenarios or inputs. These errors can lead to unexpected results or the program not functioning as intended.

How can I troubleshoot and fix issues with the logic of my program?

To troubleshoot and fix issues with the logic of a program, it can be helpful to step through the code and track the flow of the program. Using debugging tools and print statements can also help identify where the program is not following the desired logic. Once the issue is identified, it can be fixed by adjusting the code to align with the intended logic.

Similar threads

Replies
1
Views
2K
Replies
18
Views
5K
Replies
6
Views
2K
Replies
1
Views
3K
Replies
4
Views
4K
Replies
7
Views
12K
Replies
1
Views
2K
Replies
2
Views
9K
Back
Top