Regular Expression to get what is outside the square brackets

3

Sample text:

Itaú [123.456,89]

To get what's inside the brackets (including the brackets) I used:

\[(.*?)\]

The question is how to get what's out?

I suppose it's a denied list, something simple. I already searched more than hours and nothing.

If possible, I also wanted to know how to get what's inside the bracket, not including the brackets?

All in a single expression, without prolonging the code [pedals].

Sample text can be:

  

"Itaú 1 [123.456,89]", "Itaú 2 [543.456,59]", "Banco do Brasil [987.543,21]", etc.

That is, they include accents, more than one word, soon there are spaces, and it may happen to start with numbers.

    
asked by anonymous 21.08.2015 / 17:34

4 answers

2

See working at Regex .

Any clarification please ask.

Pattern:

/^([^\]]+) \[([^\]]*)\]/gm

Input:

Itau [2.265,41]
Bradesco [1.375,21]
Santander Bradesco [784,12]
Caixa 2 []

Match:

MATCH 1
1.  [0-4]   'Itau'
2.  [6-14]  '2.265,41'
MATCH 2
1.  [16-24] 'Bradesco'
2.  [26-34] '1.375,21'
MATCH 3
1.  [36-54] 'Santander Bradesco'
2.  [56-62] '784,12'
MATCH 4
1.  [64-71] 'Caixa 2'
2.  [73-73] ''
    
21.08.2015 / 19:17
3

My suggestion is to use denied classes to combine "whatever is not bracket": [^\[]+ and [^\]]+ .

Joining and putting the groups was: ([^\[]+) (\[([^\]]+)\])

Example:

var str = 'Itaú 1 [123.456,89]';
var result = str.replace(/([^\[]+) (\[([^\]]+)\])/, 'grupo 1: "$1"<br>grupo 2: "$2"<br>grupo 3: "$3"<br>');
document.body.innerHTML = result;
    
21.08.2015 / 19:04
1

In javascript, I would do the following:

To get only the word Itaú :

'Itaú [123.456,89]'.replace(/(\[.*\])/g, '');

To get what's inside the brackets (without the brackets):

'Itaú [123.456,89]'.match(/\[(.*)\]/)[1]
    
21.08.2015 / 17:35
0

See working in Regex.

link

$re = "/([A-Za-zÀ-ú0-9]+)([.,]?)/"; 
$str = "Itaú [123.456,89]"; 

preg_match_all($re, $str, $matches);
    
21.08.2015 / 18:41