Bella
Bella

Reputation: 23

Add Quotation mark to every word in string, except numbers

I have string in this format:

salary,salary,200.00,10/01/2013,

And need to format is as following:

"Salary","Salary",1580.00,"2014-11-04",

Tried with Notepad++, but can't find out how to replace for all strings, except numbers.

Upvotes: 2

Views: 787

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626870

I suggest matching and capturing what your want to avoid enclosing with double quotes (numbers) into one group, and everything else into another group. In the replacement pattern, we can specify the group that we want to enclose with double quotes using a conditional replacement pattern.

Use this regex:

(?<p1>(?:^|,)[+-]?\d+(?:[,\h]?\d{3})*(?:\.\d+)?(?:,|$))|(?<p2>[^,\r\n]+)

And replace with (?{p1}$+{p1}:"$+{p2}")

The regex contains two alternatives:

  • (?<p1>(?:^|,)[+-]?\d+(?:[,\h]?\d{3})*(?:\.\d+)?(?:,|$)) - Group p1 matching negative and positive numbers (integer or float, with digit grouping symbols as spaces or commas and decimals as . - that can be adjusted further).
  • | - or...
  • (?<p2>[^,\r\n]+) - Group p2 matching 1 or more symbols other than carriage return, line feed or comma.

The replacement pattern means: use Group p1 only if it is matched ((?{p1}$+{p1}) or use " + Group p2 + " if Group p2 is matched (:"$+{p2}")).

See screenshot:

enter image description here

Upvotes: 0

Stephan
Stephan

Reputation: 43033

Search this :

(?:^|(?<=,))([a-z]+|\d{2}/\d{2}/\d{4})(?=,)

Replace with:

"\1"

Tested on Notepad v6.6.9

Upvotes: 1

Related Questions