jmenezes
jmenezes

Reputation: 1926

Regex to search and replace whats inside parenthesis

We're trying to find text inside parenthesis and replace it with a words. In this case all text inside parenthesis, like (R:2379; L:28) etc are to be replaced with (Receipt No.:2379; Ledger No.:28)

There's that very same text on the next line that should not be touched (Don't know why it there. This is from an old DOS accounting application).

I came upto /\([R.]]+\)/g, 'Receipt No.' but this is harder than I imagined. How can this be done?

#Ch. No. 209488 #Rt. Date 12-09-1997 #Bank: Citibank (R:2379;L:28)
R:2379;L:28
#Ch. No. 884273 #Dr. Date 10-09-1997 #Ch. Dep. 14-09-1997 #Bank: Citibank (R:2432; L:28)
R:2432; L:28
#Ch. No. 884274 #Dr. Date 10-09-1997 #Ch. Dep. 19-09-1997 #Bank: Citibank (R:2475; L:28)
R:2475; L:28
#Ch. No. 884275 #Dr. Date 10-09-1997 #Ch. Dep. 24-09-1997 #Bank: Citibank (R:2480; L:28)
R:2480; L:28

Upvotes: 2

Views: 24

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626893

You can use

\(R:(\d+);\s*L:(\d+)\)

Replace with (Receipt No.:$1; Ledger No.:$2).

See the regex demo. Details:

  • \(R: - (R: text
  • (\d+) - Group 1: one or more digits
  • ; - a ; char
  • \s* - 0 or more whitespaces
  • L: - a literal L: text
  • (\d+) - Group 2: one or more digits
  • \) - a ) char.

The $1 is the backreference to Group 1 value and the $2 is the backreference to Group 2 value.

Upvotes: 1

Related Questions