Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I match none, one or two value in one string?
    text
    copied!<p>I have a string which can include none, one or two values. In case there are two value they are separated by a colon <code>:</code>, also if the first value should be empty the second (only value) would be prefixed by with a colon.</p> <p>The first value can be composed of pretty any character alphanumeric and special characters, the second value only of alphanumeric characters. Therefore a colon is not a distinct separator.</p> <p>I tried some regular expression, most of them work fine for only one value, or two values without a colon in the first but if the first one is empty the regex returns the whole string as first value.</p> <p>My actual code looks like:</p> <pre><code>if ($string =~ m/^(.+)(\:(\w+)|)$/){ $value1 = $1 || ''; if ($3){$value2 = $3} } </code></pre> <p>This one works for empty string, only first value, two values without colon in first value.</p> <p><strong>Edit</strong>:</p> <p>String could be:</p> <ul> <li><code>Hello World!</code> (only one value: value1='Hello World!')</li> <li><code>This is a test:123</code> (two values: value1='This is a test' value2='123')</li> <li><code>:Banana</code> (only one value: value2='Banana')</li> <li><code>T3-76:</code> (only one value: value1='T3-76:')</li> <li><code>Book:Title:Mysql</code> (two values: value1='Book:Title' value2='Mysql')</li> </ul> <p>The main problem is how to make a difference between a one value string which includes a colon <strong>and</strong> some text after the colon and a two values string with no colon in the first value. I should probably assume that if this case happen it is a two value string.</p> <p><strong>Edit2</strong></p> <p>If it makes it easier the code could be modified to <strong>not</strong> match an empty string.</p> <p>Anyone got an idea?</p>
 

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