# Regex: splitting conditionally on |

**URL:** <https://www.ruby-forum.com/t/regex-splitting-conditionally-on/140728>\
**Category:** Ruby\
**Created:** [June 18, 2008, 12:05am UTC](https://www.ruby-forum.com/t/regex-splitting-conditionally-on/140728 "2008-06-18T00:05:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![computorist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9d8465/32.png) [@computorist](https://www.ruby-forum.com/u/computorist)\
**Post date:** [June 18, 2008, 12:05am UTC](https://www.ruby-forum.com/t/regex-splitting-conditionally-on/140728/1 "2008-06-18T00:05:45Z")

</div>

I’m parsing mediawiki markup and I’d like to split a multi-line string  
on | (vertical-bar), but only if it isn’t contained w/in another  
pattern.

| name1 = value1  
| name2 = value2 | name3 = value3  
| name4 = [[foo|bar]]

I’d like String.split(/pattern/) to return [‘name1 = value1’,‘name2 =  
value2’,‘name3 = value3’,‘name4 = [[foo|bar]]’]

I’ve working on a negative look-ahead pattern without success and my  
brain is tired.

Thanks for any suggestions

---

<div class="post-metadata">

**Author:** ![computorist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9d8465/32.png) [@computorist](https://www.ruby-forum.com/u/computorist)\
**Post date:** [June 18, 2008, 8:40am UTC](https://www.ruby-forum.com/t/regex-splitting-conditionally-on/140728/2 "2008-06-18T08:40:43Z")

</div>

On 18.06.2008 00:04, computorist wrote:

> I’ve working on a negative look-ahead pattern without success and my  
> brain is tired.
> 
> Thanks for any suggestions

Without testing, something like this might work:

str.scan %r{  
(?: [[^]]\* ] | [^|] )+  
}xm

Cheers

robert

---

<div class="post-metadata">

**Author:** ![computorist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9d8465/32.png) [@computorist](https://www.ruby-forum.com/u/computorist)\
**Post date:** [June 18, 2008, 9:37am UTC](https://www.ruby-forum.com/t/regex-splitting-conditionally-on/140728/3 "2008-06-18T09:37:19Z")

</div>

computorist wrote:

> I’ve working on a negative look-ahead pattern without success and my  
> brain is tired.
> 
> Thanks for any suggestions

str.split /\s\*|\s\*(?=\s\*\w+\s\*=)/  
I don’t know whether this exactly meets your requirement, but this will  
split  
only on |s that are followed by a word and a =. For your sample input  
that  
gives the desired result.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![computorist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9d8465/32.png) [@computorist](https://www.ruby-forum.com/u/computorist)\
**Post date:** [June 18, 2008, 7:25pm UTC](https://www.ruby-forum.com/t/regex-splitting-conditionally-on/140728/4 "2008-06-18T19:25:59Z")

</div>

On Jun 18, 12:35 am, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > value2’,‘name3 = value3’,‘name4 = [[foo|bar]]’]
> 
> ## HTH, Sebastian
> 
> Jabber: removed\_email\_address@domain.invalid  
> ICQ: 205544826

This seems to work well. Searching for what I want is better than  
excluding the bits I don’t want.

Thanks
