Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The easiest way (least lines of code) to do this would be to use a regular expression. For instance, to find all of that strings enclosed in pointy brackets, you could use this regular expression:</p> <pre><code>\&lt;(?&lt;value&gt;.*?)\&gt; </code></pre> <p>Here's what that all means:</p> <ul> <li><code>\&lt;</code> - Find a string which starts with a <code>&lt;</code> character. Since <code>&lt;</code> has a special meaning in RegEx, it must be escaped (i.e. preceded with a backslash)</li> <li><code>(?&lt;value&gt;xxx)</code> - This creates a named group so that we can later access this portion of the matched string by the name <code>"value"</code>. Everything contained in the name group (i.e. where <code>xxx</code> is), is considered part of that group.</li> <li><code>.*?</code> - This means find any number of any characters, up to, but not including whatever comes next. The <code>.</code> is a wildcard which means any character. The <code>*</code> means any number of times. The <code>?</code> makes it non-greedy so it stops matching as soon as if finds whatever comes next (the closing <code>&gt;</code>).</li> <li><code>\&gt;</code> - Specifies that matching strings must end with a <code>&gt;</code> character. Since <code>&gt;</code> has a special meaning in RegEx, it must also be escaped.</li> </ul> <p>You could use that RegEx expression to find all the matches, like this:</p> <pre><code>Dim items As New List(Of String)() For Each i As Match In Regex.Matches(source, "\&lt;(?&lt;value&gt;.*?)\&gt;") items.Add(i.Groups("value").Value) Next </code></pre> <p>The trick to making it work in your scenario is that you need to dynamically specify the opening and closing characters. You can do that by concatenating them to the RegEx, like this:</p> <pre><code>Regex.Matches(source, opener &amp; "(?&lt;value&gt;.*?)" &amp; closer) </code></pre> <p>But the problem is, that will only work if <code>source</code> and <code>closer</code> are not special RegEx characters. In your example, they are <code>&lt;</code> and <code>&gt;</code>, which are special characters, so they need to be escaped. The safe way to do that is to use the <code>Regex.Escape</code> method, which only escapes the string if it needs to be:</p> <pre><code>Private Function GetClosedText(source As String, opener As String, closer As String) As String() Dim items As New List(Of String)() For Each i As Match In Regex.Matches(source, Regex.Escape(opener) &amp; "(?&lt;value&gt;.*?)" &amp; Regex.Escape(closer)) items.Add(i.Groups("value").Value) Next Return items.ToArray() End Function </code></pre> <p>Notice that in the above example, rather than finding a single item and returning it, I changed the <code>GetClosedText</code> function to return an array of strings. So now, you can call it like this:</p> <pre><code>ListBox1.Items.AddRange(GetClosedText(TextBox1.Text, TextBox3.Text, TextBox4.Text)) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload