multiple pattern split in perl

Hello all
i have complex string that i will like to split to or to break to parts
my string is :
"foo1+foo22-blahblah(var)+fooA(var)"
now i need to break this string so that every part of the string will be element in array it will b
@stack[0] = "foo1"
@stack[1] = "+"
@stack[2] = "foo22"
@stack[3] = "-"
@stack[4] = "blah"
@stack[5] = "
"
@stack[6] = "blah(var)"
@stack[7] = "+"
@stack[8] = "fooA(var)"

now the problem as you see is the different separators , if i had only one type so i could split this string
by this one type pattern and build that array but here i have multiple type (+|-|*) what will be the best
why to build this array in this case?

1 Like
$, = "\n"; 
print split(/([-+*])/, "foo1+foo22-blah*blah(var)+fooA(var)")';
foo1
+
foo22
-
blah
*
blah(var)
+
fooA(var)
1 Like

hello and thanks for the fast reply
This is great solution I didn't know perl can take multiple patterns for separation
But one thing remains I also need to capture the patterns and put them into the array
What I mean is when I do the split I actually don't get the pattern, how can I keep it and push it to the array?

1 Like

Did you really run my example? If you did, you should find that the separators are in the array returned, and the output array is exactly what you indicated in your post. Verify it.

1 Like