laggingreflex
laggingreflex

Reputation: 34627

Regex to parse the first letter of each line?

This is the list:

Work
Work
Fire
Global

And I want to extract the string WWFG from it. [(?).*\n] just give me Global. What should I rather be using?

For context, I'm using Rainmeter's webparser plugin.

Upvotes: 2

Views: 1885

Answers (7)

laggingreflex
laggingreflex

Reputation: 34627

(?siU)(?(?=.)(.))(?(?=.*\n).*\n(.))(?(?=.*\n).*\n(.))(?(?=.*\n).*\n(.))

Answered by @moshi here. And it works perfectly with Rainmeter.

Upvotes: 1

lhf
lhf

Reputation: 72312

If that is Lua, try s:gsub("(.).-\n","%1").

Upvotes: 0

Egor Skriptunoff
Egor Skriptunoff

Reputation: 23727

Try this: (?simU)^(.)
RainRegExp seems to lack the replacement feature, so it is impossible to get all the captures concatenated into one string.

Upvotes: 2

garyh
garyh

Reputation: 2852

This will capture the first character from each line

([a-z])[^\n]+\n*

the replace with \1 or $1

Depending on what is in the text you might need to change [a-z] to something more all-encompassing

Upvotes: 0

Chris Martin
Chris Martin

Reputation: 30736

I have no idea what language you're using, but here's some Python!

>>> import re
>>> ''.join(re.findall("(.).*", "Work\nWork\nFire\nGlobal"))
'WWFG'

Upvotes: 0

Andrew Cheong
Andrew Cheong

Reputation: 30273

The easiest way depends on what language you're using, but you want to replace

(.).*\n

with

$1

Upvotes: 1

Firas Dib
Firas Dib

Reputation: 2621

You need to use the multiline flag with an anchor. I would use: /^(.)/gm (syntax differs from language to language)

See example here: http://regex101.com/r/uC1gV5

Upvotes: 1

Related Questions