Home > Blockchain >  Regex formula for following expression
Regex formula for following expression

Time:01-15

I have a an array which are separated by commas, i want each word to be replaced with double quotes(for each and every word) as shown below

--> a1.large,a2.large,a3.large,b4.medium

--> "a1.large","a2.large","a3.large","b4.medium"

can anyone tell how to do it in notepad using find and replace using regex.

Thanks!

CodePudding user response:

In notepad , do the following:

  • CTRL H
  • Set the Search mode:Regular Expression
  • Find (\w (?:\s*\w )*\s*(?=,|$))
  • Replace the matches with "$1"

Explanation:

  • \w - matches 1 or more word characters, as many as possible
  • (?:\s*\w )* - matches 0 or more occurrences of 0 spaces followed by 1 word characters. This submattern will allow us to match multiple words separated by white-spaces between commas.
  • \s* - match 0 or more white spaces
  • (?=,|$) - positive lookahead that matches the current position if it is followed either by a , or by end of the line
  • () - everything matched so far will be captured in Group 1

Demo

enter image description here

CodePudding user response:

Match words and replace with quoted match:

Find: \w 
Replace: "$0"

If multiple words are possible between commas, eg "car, car keys, wallet" change find regex:

Find: \w (  \w )*
Replace: "$0"

$0 is group zero, which is the entire match.

CodePudding user response:

  • Ctrl H
  • Find what: [^,\r\n]
  • Replace with: "$0"
  • CHECK Wrap around
  • CHECK Regular expression
  • Replace all

Explanation:

[^,\r\n]        # 1 or more any character that is not a comma or linebreak

Replacement:

"$0"            # the whole match surrounded with quotes

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

CodePudding user response:

Find: [\w.]*
Replace All:"$0"

  •  Tags:  
  • Related