Marcin D
Marcin D

Reputation: 1

Regular expression for matching pairs

I'm trying to write regular expression in java for matching pairs (param : value) from specific text:

"CUSTOMER

Customer Name               :  JOHN DOE

City                  :  Chicago

Customer Id               :  123AWE32D2

System Id             :  004349

Register Date (yymmddww)      :  12020906 

"

As output I want to have pairs:

Customer Name, JOHN DOE City, Chicago Customer Id, 123AWE32D2 System Id, 004349 Register Date (yymmddww), 12020906

There could be various number of parameters. I need to have universal pattern for extracting groups (param name) : (value). Here is my pattern, I wrote for matching specific param:

\s*Customer Name\s*:\s*([^\n]*\S)

Upvotes: 0

Views: 595

Answers (3)

CodeHelp
CodeHelp

Reputation: 1328

You can try this

Matcher m = Pattern.compile("(?s)\\b(.+?):(.*?)\\b(.+?)\\n").matcher(your text);
while (m.find())
    System.out.print(m.group(1).trim() + "," + m.group(3).trim() + " ");

Upvotes: 0

Bohemian
Bohemian

Reputation: 425198

Use the "multi line" switch (?m) that makes ^/$ match after/before newlines:

(?m)^\\s*(.+?)\\s*:\\s*(.+?)\\s*$

Each match will capture the param name in group 1 and the value in group 2.

Upvotes: 0

Pawel
Pawel

Reputation: 1467

1) Use this (.+):(.+) in multiline mode.

Meaning:

. - anything

: - then we need colon

. - again anything

+ - at least one character

You have 2 groups, before colon and after.

2) or simply use split() method - split(":")

After both, use trim() to remove spaces or remove them in regular expression.

Upvotes: 2

Related Questions