<span>Hi,<br />
I' m trying to extract a name from a string that starts and ends with a <pre><code class="language-igor"><</code></pre> and <pre><code class="language-igor">></code></pre> which I'm trying to get rid of. I've been using <pre><code class="language-igor">sscanf </code></pre>and it works for the lesser than sign at the beginning but not the other sign at the end. <br />
<br />
<pre><code class="language-igor">
function test()
string name
sscanf "<Signal001>", "%*[<]%s%*[>]", name
print name
end
</code></pre><br />
As a result I get: <pre><code class="language-igor">Signal001></code></pre><br />
<br />
Any suggestions ?</span>
I've been using sscanfand it works for the lesser than sign at the beginning but not the other sign at the end.
Others already posted versions which work, so I'll just focus on why this does not work.
The reason is that the "%s" specifier also eats the ">". In your case you could get away with
function test()
string name sscanf"<Signal001>", "<%[A-Za-z0-9]>", name print name end
but I personally lean towards the regexp as that is the most generic solution.
Sounds like you have a good solution using regular expressions, but the ReplaceString function is also very easy to use, assuming the "<" and ">" characters would never show up in the name.
String s0="<Signal001>"
string name
String regExp="(?<=<)(.+)(?=>)"
SplitString/E=regExp s0,name
print name
end
November 15, 2017 at 04:43 am - Permalink
string name = "<Signal001>"
name = name[1, strlen(name) - 2]
print name
end
In
name[1, strlen(name) - 2]
, the "1" and "-2" are required to remove the first and last characters because string indexing is zero based in Igor.November 15, 2017 at 06:17 am - Permalink
Others already posted versions which work, so I'll just focus on why this does not work.
The reason is that the "%s" specifier also eats the ">". In your case you could get away with
string name
sscanf "<Signal001>", "<%[A-Za-z0-9]>", name
print name
end
but I personally lean towards the regexp as that is the most generic solution.
November 15, 2017 at 07:11 am - Permalink
November 15, 2017 at 11:29 am - Permalink
November 17, 2017 at 02:51 pm - Permalink