nᵗʰ出现时正则表达式分裂

时间:2013-03-30 00:45:00

标签: regex

我有一个存储在Hive中的字符串,我想在,(或任何其他字符)的4 th 出现时拆分文本。

如果有人能给我提示正则表达式,我真的很感激。

案文如下:

  

The Band,The Band,Up On Cripple Creek(2000 Digital Remaster),2000,Greatest Hits,The Band ,, The Weight(2000 Digital Remaster),2003,The Rhythm Of The Rain,The Cascades ,, Rhythm Of雨(LP版),2005年,纪事第一卷,Creedence Clearwater复兴,谁将停止雨,1976年,完整太阳单打,第一卷。 1,Johnny Cash,I Walk the Line,2001,Greatest Hits,Bob Seger ,, Against the Wind,1980,他们最伟大的歌曲,老鹰,Lyin'Eyes,1975,Johnny Horton的精选,Johnny Horton,North到阿拉斯加,1987年,Super Hits,Marty Robbins,你给了我一座山,1969年,最伟大的命中,Bob Seger,夜行动,1976年,你好Darlin'15#1 Hits,Conway Twitty,它只是让人相信,2003 ,Anthology,Kenny Rogers&第一版,红宝石,不要把你的爱带到镇上,1996年,最伟大的命中,尼尔年轻,老人,2004年,收获,尼尔年轻,黄金之心,2009年,最好的,斯普林菲尔德, ,银线和金针,2011年,最好的Statler兄弟,Statler兄弟,Susan当她尝试,1987年,最终收藏,Statler Brothers,The Class Of '57,2005,The Definitive Collection,The Statler兄弟,我会去我的坟墓爱你,2005年,最伟大的命中:1974-1978,Steve Miller Band,The Joker,2006,Greatest Hits:1974-1978,Steve Miller Band ,, Rock'n Me, 2006年,早期女孩7“Hits,Gale Garnett,我们将在阳光下歌唱,2010年,道路之王,各种艺术家,我不能停止爱你 - Don Gibson,2004,America's Troubador,Willie Nelson, ,天使飞向地面,2005年,他们最伟大的命中,老鹰,拿它到极限,1975年,他们的最伟大的命中,老鹰,Desperado,1973年,公路员,公路员,Desperados等火车,1985,Super Hits,Marty Robbins,我的女人,我的女人,我的Wi fe,1970,Super Hits,Marty Robbins,一些记忆不会死,1982年,Highwayman,The Highwaymen,致力于Parkview,1985年,Greatest Hits - Roy Clark,Roy Clark,昨天我年轻时,1995 ,Greatest Hits - Roy Clark,Roy Clark,I Never Picked Cotton,1995,Simon& Garfunkel的精彩片段,Simon& Garfunkel,Bridge Over Troubled Water [Live],1970,Collection,The Oak Ridge Boys ,, Y'all Come Back Saloon,1977,Super Hits,Vern Gosdin ,, Chiseled In Stone,1987,Super Hits,Vern Gosdin ,,你应该责怪谁,1987年,最好的约翰丹佛[光盘2],约翰丹佛,落基山高,1972年,最好的约翰丹佛[光盘2],约翰丹佛,带我回家,Country Roads,1971,纪念品,Vince Gill,Never Knew Lonely,1995,Souvenirs,Vince Gill,当我打电话给你的名字,1995年,纪念品,Vince Gill,口袋里满是金色,1995年,Greatest Hits - Waylon Jennings ,Waylon Jennings,Bob Wills仍是King,2000,Greatest Hits - Waylon Jennings,Waylon Jennings,Just To Satisfy You,2000

3 个答案:

答案 0 :(得分:0)

The Regex ......

/         # Start regex
(         # Start group
[^,]*,    # not `,` - zero or more times (*) followed by `,`
){4}      # repeat group four times
/g        # match globally (using String.match)

一起......

console.log( str.match(/([^,]*,){4}/g) );

答案 1 :(得分:0)

^([^,]*,[^,]*,[^,]*,[^,]*,)(.*)$

应该给你两个字符串,1)直到并包括第四个逗号的部分,以及2)字符串的其余部分。

答案 2 :(得分:0)

用逗号分割,然后再次加入一个简单的循环......

var arr, i, out, str;

str = "The Band,The Band,,Up On Cripple Creek (2000 Digital Remaster),2000,Greatest Hits,The Band,,The Weight (2000 Digital Remaster),2003,Rhythm Of The Rain,The Cascades,,Rhythm Of The Rain (LP Version),2005,Chronicle Volume One,Creedence Clearwater Revival,,Who'll Stop the Rain,1976,The Complete Sun Singles, vol. 1,Johnny Cash,,I Walk the Line,2001,Greatest Hits,Bob Seger,,Against The Wind,1980,Their Greatest Hits,The Eagles,,Lyin' Eyes,1975,Johnny Horton's Greatest Hits,Johnny Horton,,North To Alaska,1987,Super Hits,Marty Robbins,,You Gave Me A Mountain,1969,Greatest Hits,Bob Seger,,Night Moves,1976,Hello Darlin' 15 #1 Hits,Conway Twitty,,It's Only Make Believe,2003,Anthology,Kenny Rogers & The First Edition,,Ruby, Don't Take Your Love To Town,1996,Greatest Hits,Neil Young,,Old Man,2004,Harvest,Neil Young,,Heart Of Gold,2009,The Very Best Of,The Springfields,,Silver Threads And Golden Needles,2011,The Best Of The Statler Brothers,The Statler Brothers,,Susan When She Tried,1987,The Definitive Collection,The Statler Brothers,,The Class Of '57,2005,The Definitive Collection,The Statler Brothers,,I'll Go To My Grave Loving You,2005,Greatest Hits: 1974-1978,Steve Miller Band,,The Joker,2006,Greatest Hits: 1974-1978,Steve Miller Band,,Rock'n Me,2006,Early Girl 7\" Hits,Gale Garnett,,We'll Sing In The Sunshine,2010,King of the Road,Various Artists,,I Can't Stop Loving You - Don Gibson,2004,America's Troubador,Willie Nelson,,Angel Flying To Close To The Ground,2005,Their Greatest Hits,The Eagles,,Take It To The Limit,1975,Their Greatest Hits,The Eagles,,Desperado,1973,Highwayman,The Highwaymen,,Desperados Waiting For A Train,1985,Super Hits,Marty Robbins,,My Woman, My Woman, My Wife,1970,Super Hits,Marty Robbins,,Some Memories Just Won't Die,1982,Highwayman,The Highwaymen,,Committed To Parkview,1985,Greatest Hits - Roy Clark,Roy Clark,,Yesterday When I Was Young,1995,Greatest Hits - Roy Clark,Roy Clark,,I Never Picked Cotton,1995,Simon & Garfunkel's Greatest Hits,Simon & Garfunkel,,Bridge Over Troubled Water [Live],1970,Collection,The Oak Ridge Boys,,Y'all Come Back Saloon,1977,Super Hits,Vern Gosdin,,Chiseled In Stone,1987,Super Hits,Vern Gosdin,,Who You Gonna Blame It On This Time,1987,The Very Best Of John Denver [Disc 2],John Denver,,Rocky Mountain High,1972,The Very Best Of John Denver [Disc 2],John Denver,,Take Me Home, Country Roads,1971,Souvenirs,Vince Gill,,Never Knew Lonely,1995,Souvenirs,Vince Gill,,When I Call Your Name,1995,Souvenirs,Vince Gill,,Pocket Full Of Gold,1995,Greatest Hits - Waylon Jennings,Waylon Jennings,,Bob Wills Is Still King,2000,Greatest Hits - Waylon Jennings,Waylon Jennings,,Just To Satisfy You,2000";

arr = str.split(/,/);

out = [];

i = 0;

while (i < arr.length - 4) {
  i += 4;
  out.push("" + arr[i] + ", " + arr[i + 1] + ", " + arr[i + 2] + ", " + arr[i + 3]);
}

console.log(out);
相关问题