echapp
echapp

Reputation: 15

RegEx - Finding a string between pattern

I'm new to Regex and I'm struggling in finding a string inside a pattern.

I have this string:

{"linha":""},
{"linha":"              REDE GETNET"},
{"linha":"               SANTANDER"},
{"linha":""},
{"linha":"20/04/15 09:07:32 AUT:006299 DOC:000235"},
{"linha":"EC:000000000370484 TERM: T0385403    M"},
{"linha":"CV:010000024       CAIXA:00003333"},
{"linha":""},
{"linha":"CARTAO              ************2125"},
{"linha":""},
{"linha":"            CREDITO A VISTA"},
{"linha":"VALOR:        12,00"},
{"linha":""},
{"linha":"  ______________________________"},
{"linha":"         ASSINATURA"},
{"linha":""}, 
{"linha":""},
{"linha":"CUPOM: 00000000000000        MAC: 9235"},
{"linha":"NSU_CTF: 001899  LOJA: 0019  PDV: 897"},
{"linha":""},
{"linha":""}

I'd like to find the ocurrences between:

{"linha":

and

},

Getting only the string in double quotes after colon.

Until now my regex is:

(\{".*(linha).[:])

and it's getting only

{"linha":

Can someone help me? I intend doing it in javascript.

Upvotes: 0

Views: 88

Answers (2)

cypher
cypher

Reputation: 449

Solutions in Javascript:

Using Regex:

var input_str = '{"linha":""},{"linha":"              REDE GETNET"},{"linha":"               SANTANDER"},{"linha":""},{"linha":"20/04/15 09:07:32 AUT:006299 DOC:000235"},{"linha":"EC:000000000370484 TERM: T0385403    M"},{"linha":"CV:010000024       CAIXA:00003333"},{"linha":""},{"linha":"CARTAO              ************2125"},{"linha":""},{"linha":"            CREDITO A VISTA"},{"linha":"VALOR:        12,00"},{"linha":""},{"linha":"  ______________________________"},{"linha":"         ASSINATURA"},{"linha":""}, {"linha":""},{"linha":"CUPOM: 00000000000000        MAC: 9235"},{"linha":"NSU_CTF: 001899  LOJA: 0019  PDV: 897"},{"linha":""},{"linha":""}'
var re = new RegExp('linha*\":(".*")', 'g'); 
var myArray;
while ((myArray = re.exec(input_str)) !== null) {
  var msg = 'Found ' + myArray[1];
  console.log(msg);
}

Using JSON.parse: Notice that i have added [] to wrap the string as JSON Array.

var input_str1 = '[{"linha":""},{"linha":"              REDE GETNET"},{"linha":"               SANTANDER"},{"linha":""},{"linha":"20/04/15 09:07:32 AUT:006299 DOC:000235"},{"linha":"EC:000000000370484 TERM: T0385403    M"},{"linha":"CV:010000024       CAIXA:00003333"},{"linha":""},{"linha":"CARTAO              ************2125"},{"linha":""},{"linha":"            CREDITO A VISTA"},{"linha":"VALOR:        12,00"},{"linha":""},{"linha":"  ______________________________"},{"linha":"         ASSINATURA"},{"linha":""}, {"linha":""},{"linha":"CUPOM: 00000000000000        MAC: 9235"},{"linha":"NSU_CTF: 001899  LOJA: 0019  PDV: 897"},{"linha":""},{"linha":""}]'
var parsed_json = JSON.parse(input_str1); 
console.log(parsed_json);
parsed_json.forEach(x => console.log(x.linha))

Upvotes: 1

Matteo Ragni
Matteo Ragni

Reputation: 2956

As far as I can see the example is JSON, thus you should use JSON methods to get the data you want. BUT, if for any reason you are "forced" to use regular expression, than something like that may do the job:

/\{\s*\"linha\"\s*\:\s*\"(.*)\"\}/g

You can test it here, and it is also robust with respect to (at least) {"linha": " \" \" "} and it also matches { "linha": ""}. For sure there are some cases in which this regex will not work correctly (for example, it will not get numeric values, only strings).

Thus, again, you should really check out JSON. It is amazing! :)

Upvotes: 1

Related Questions