Reputation: 2736
Can't figure this one out at all.
I have the following string aaa||bbb||ccc
. I'd like to split by ||
.
I had a Regex that worked for aaa||bbb
which was (.*)\|\|(.*)
but that doesn't work for 3 values or more.
I need one to work for unlimited number of values.
Thanks
EDIT: Sorry forgot to mention. I'm using Ant with PropertyRegex task from Ant-Contrib library.
So basically just need the plain regex.
Upvotes: 2
Views: 2653
Reputation: 10377
you may use the split function from Ant Addon Flaka, f.e. :
<project name="demo" xmlns:fl="antlib:it.haefelinger.flaka">
<property name="somestring" value="aaa||bbb||ccc"/>
<fl:let>counter = 0</fl:let>
<fl:for var="substring" in="split('${somestring}','\|\|')">
<fl:let>counter = counter + 1</fl:let>
<!-- simple echo -->
<fl:echo>substring #{counter} => #{substring}</fl:echo>
<!-- create properties for further processing -->
<fl:let>substring#{counter} := '#{substring}'</fl:let>
</fl:for>
<echo>
$${substring1} => ${substring1}
$${substring2} => ${substring2}
$${substring3} => ${substring3}
</echo>
</project>
output :
Buildfile: /home/rosebud/AntTest/tryme.xml
[fl:echo] substring 1 => aaa
[fl:echo] substring 2 => bbb
[fl:echo] substring 3 => ccc
[echo] ${substring1} => aaa
[echo] ${substring2} => bbb
[echo] ${substring3} => ccc
Upvotes: 0
Reputation: 2991
How about a variation of the following:
[^|]+(?:\|\|[^|]+)*
1 2 1
Where 1
matches anything but a pipe and 2
matches your separator?
Matches-->aaa
Matches-->aaa||bbb
Matches-->aaa||bbb||ccc
Matches-->aaa||bbb||ccc||ddd
Does not match-->aaa||
Does not match-->aaa|bbb
Does not match-->aaa|bbb|ccc
Upvotes: 1
Reputation: 1
The below code will resolve this
string x1 = "aaa||bbb||ccc";
string[] result = Regex.Split(x1, @"\|\|");
you can use this too
string[] parts = x1.Split(new string[] { "||" }, StringSplitOptions.None);
Upvotes: 0
Reputation: 183251
You can't do that with a plain regex-match; rather, you need to use a "split" function of some stripe. This will depend on the language you're using. For example, in Java you could write:
final String[] fields = "aaa||bbb||ccc".split("[|][|]", -1);
or in Perl:
my @fields = split /\|\|/, 'aaa||bbb||ccc', -1;
If you update your question to indicate what language you're using, you'll get a more helpful answer.
Upvotes: 0
Reputation: 454960
Instead of matching and using captured groups, you can split the string on ||
using the regex:
\|\|
Upvotes: 3