user98293989823
user98293989823

Reputation: 99

Find line that starts with something, and in this line find word

i would like to know if is possible to first find a line that starts with some specific word, than in this line, search for other world.

I have this regex http://www.regexr.com/3bsl1

String

Empregados/Avulsos 0,00 0,00 0,00 0,00 0,00
Contribuintes Individuais 0,00 0,00 0,00 0,00 0,00
RAT 34.792,90 0,00 0,00 0,00 34.792,90
RAT - Agentes Nocivos 0,00 0,00 0,00 0,00 0,00
Valores Pagos a Cooperativas 0,00 0,00 0,00 0,00 0,00
Adicional Cooperativas 0,00 0,00 0,00 0,00 0,00
Comercialização Produção 0,00 0,00 0,00 0,00 0,00
Evento Desportivo/Patrocínio 0,00 0,00 0,00 0,00 0,00
COMP 07 Empregados/Avulsos INSCRIÇÃO bla ble bli 082 sk3$ ^# x sms
COMP 0 283 20 3882 03

Regex

/^COMP\s*?(Empregados/Avulsos)/

So the goals here is to find the line that starts with COMP, which is working, than immediately jump to Empregados/Avulsos, but is not working this part.

Thanks.

Upvotes: 0

Views: 625

Answers (2)

GregL
GregL

Reputation: 38121

If you use a non-capturing group, you can match the starting part of the line, "COMP", and then just capture the bit you want, which is the Empregados/Avulsos part.

The regex looks like this:

/(?:^COMP.*)(Empregados/Avulsos)/gm

That way, the only match, $1 equals "Empregados/Avulsos".

Try it out on Regexr

Upvotes: 2

Marcos Casagrande
Marcos Casagrande

Reputation: 40414

Test this:

/^COMP.*?(Empregados\/Avulsos)/gm

Working example: http://www.regexr.com/3bsla

Note: Always escape special characters like /.[] etc.

Upvotes: 2

Related Questions