2017-08-02 51 views
1

显然这并不容易!有必要使用纯正则表达式?我只知道有....简单一点:REGEX在多行文本中选择' - '后的所有内容

我已经找到一种方法来选择一个连字符在文本文件中

Unique Thing - Some Text 
Another Thing - Some Text again 
Some Thing - Some more text 

但我只想要连字符的右侧第一次出现后的文本.. 任何人都知道一个快速的正则表达式来完成这个?

需要明确的是,鉴于上面的文字,我想

Some Text 
Some Text again 
Some more text 

感谢你们大家

UPDATE: 也许这将帮助文本的实际块。这是来自2017年8月2日白宫新闻发布会的最新直播聊天室。

Hernando Arce - build the wall with solar panels, 
Christmas Girl - Let's do our own quick internet poll on live chat. Ready........Good with new immigrating into the US policy he is talking about. YES or NO, 
ART - AMEN, 
coffeefish - Stop H1B visa corruption!, 
CarollDelMuro .Arbonne - Red, 
Legion - BUILD THE WALL!, 
wass sabi - MAGA, 
Yokoshima - I live in Florida. Speaking English isn't racist. If you've ever been to Miami, you would know why it's needed., 
Home O'DFree - NO the campaign was BUILD THE WALL, 
Melissa Renee - is he on benzos, 
Paid Observer - kim jung un vs Trump in basketball, 
Selina Serrano - polling data, 
zonnekat - aliens...., 
Farrah - NFL , 
Selina Serrano - massive, 
Glenda Greene - MAGA, 
Christoph Schneider - who would ever come to USA when they get lower pays? Russians?, 
Carolyn Hall - MAGA MAGA MAGA , 
Sandra Honeyman - Isn't limiting immigration to skilled workers going to displace more skilled American workers?, 
Mike Hancock - AMERICA FIRST, 
Adnan Khan - Send them back to Mars, 
Paid Observer - wtf is that, 
GDotcom - THIS BETTER PASS OR THERE SHALL BE HANGINGS, 
Null_Mage - This man is more attractive than Sarah, 
monkeygraborange - FUCK CONGRESS, 
Selina Serrano - personal, 

这是我在regex101中测试的文本。 ^[^-]*[^ -]在这里似乎不起作用。

我喜欢关于一行一行地分割然后匹配的一些建议,聊天流是成千上万行。所有这些的最终结果是计算单词的出现次数。对于任何有兴趣回购的客户,我只是在最新的新闻发布会上推送日志。

const r = /- (.*)/ 
console.log('Unique Thing - Some Text'.match(r)[1]) //'Some Text' 
+2

为什么要使用正则表达式? 'str.split(“ - ”)[1]'应该可以工作。 –

+0

你的代码在哪里?什么不行? – Toto

+0

我刚刚在正则表达式101中进行了测试。虽然好点。我会尝试你的建议! – archae0pteryx

回答

2

/[\s\S]- (.*)/g - 应做它

[\s\S] - 匹配新行 /g - 继续匹配

3

,如果你想使用正则表达式您可以使用捕获组。

有了这个模式,你可以删除所有匹配的文本,并离开你想要的右侧。

UPDATE:

但是,如果你想在右边你可以使用它:

-(.*) 

,并选择1组:

enter image description here

+0

这只会选择第一行。我有数千行。这是我的问题。 – archae0pteryx

+0

所以你可以拆分文本:text.split('\ n')。map(line => line.match(r)[1]) – csander

+0

哦。这是一个好主意!谢谢。 – archae0pteryx

2

试试这个:

.*-[ ]* 

选择连字符后的连字符和空格前的一切

+0

不。这只会选择连字符之前的第一个块并包括连字符 – archae0pteryx

+0

这也适用! – archae0pteryx