Mars Moon
Mars Moon

Reputation: 114

Regular Expression to extract data between two bracket

For example I have a string line as follows

What is your family Occupation? [RMB 2011]

I want to extract two data from here.

The RMB part can be of any length. I have used following regex pattern to extract the data

^[(\[][A-Z]{3}\s\d\d\d\d[)\]]$

But it does not match anything in the string. Please let me know what I am doing wrong.

Upvotes: 0

Views: 60

Answers (1)

xingbin
xingbin

Reputation: 28279

You do not need the string start with ]. Try this online:

\[([A-Z]{3})\s(\d{4})\]

And if RMB part can be of any length, you can change [A-Z]{3} to [A-Z]{1,}.

Upvotes: 3

Related Questions