-1

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

如果有人能给我关于正则表达式的提示,我将不胜感激。

正文如下:

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版),2005,编年史第一卷,Creedence清水复兴,,谁来停雨,1976,完整的太阳单曲,卷。1,约翰尼·卡什,,我走这条线,2001,精选,鲍勃·西格,,逆风,1980,他们的精选,老鹰队,,Lyin' Eyes,1975,约翰尼·霍顿的精选,约翰尼·霍顿,北去阿拉斯加,1987 年,超级热门歌曲,马蒂·罗宾斯,,你给了我一座山,1969 年,最伟大的歌曲,鲍勃·西格,,夜间行动,1976 年,你好,达林 15 #1 次热门歌曲,康威·特威蒂,,这只是让人相信,2003 年,选集,肯尼·罗杰斯和第一版,,鲁比,不要把你的爱带到镇上,1996,最热门歌曲,尼尔杨,,老人,2004,收获,尼尔杨,,黄金之心,

4

3 回答 3

0

正则表达式...

/         # 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) );
于 2013-03-30T00:50:01.673 回答
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);
于 2013-03-30T01:35:41.747 回答
0
^([^,]*,[^,]*,[^,]*,[^,]*,)(.*)$

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

于 2013-03-30T00:59:38.520 回答