Need regex for separator

what regex would I use to get separate values on a pipe ("|") separator? It has to be a regex, not a split etc.

Example:

abc|def|ghi
abc|def|ghi|jkl
123|456|789
123|456|789|101112
one1|two2|3three|4four|five5
894359Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Terry WoodsIT GuruCommented:
You could do it with a single regex if you had a fixed limit of the number of values.
eg something like this:
/^([^|]*)\|?([^|]*)\|?([^|]*)\|?([^|]*)\|?/

If you had essentially a while loop or recursive function, you could keep matching with the pattern:
/^([^|]*)\|(.*)/

Open in new window

and add the first match to an array then repeat the above on the 2nd match.
0
Terry WoodsIT GuruCommented:
Anyway, what's wrong with a split? Do you need to handle escaped | characters or something like that?
0
RobOwner (Aidellio)Commented:
Whether you do a reflex or split you'll end up with an array of your values. Please explain the reason behind not so putting the array. You can always join it back together later if you need to
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

894359Author Commented:
i'm using a tool which requires a regex. What about this:

^.+\|(.+)\|(.+)|(.+) and I take $2? I'm just looking to identify the third segment of a pipe delimited string.
0
RobOwner (Aidellio)Commented:
That makes sense. Ignore my last comment
0
894359Author Commented:
how do I account for situations where there may at times be additional pipes after 2nd one? I only want the third segment

^.+\|(.+)\|(.+)\|(.+)

abc|def|ghi
abc|def|ghi|jkl
123|456|789
123|456|789|101112
one1|two2|3three|4four|five5
0
RobOwner (Aidellio)Commented:
See here, you'll want the global identifier and then acces the array via the [2]

http://jsbin.com/eneyORE/1/edit
0
894359Author Commented:
Hi Tagit,
How do I get it to run here http://regexpal.com/
0
Terry WoodsIT GuruCommented:
Tagit's pattern is the equivalent of using:
[^|]+
on http://regexpal.com/ and taking the 3rd match.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobOwner (Aidellio)Commented:
Thanks Terry
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.