How to find and replace in Notepad++ while before exact text? - regex

I have a file that contains thousands of lines (json columns) like this:
"128",
"drugName_en": "Ampy 500mg Capsules",
"drugName_ar": "امبي 500 مجم كبسول",
"scientificDrugId": "959",
"proxyDrugId": "01",
"prodCompDrugId": "06",
"pack": "2x10Capsules",
"unit": "باكت",
"price": "0",
"discount": "00",
"categoryId": "15",
"image": "http://icons.iconarchive.com/icons/graphicloads/medical-health/256/medicine-box-icon.png"
},
"129",
"drugName_en": "Ampy C 10*10 C 500mg Capsules",
"drugName_ar": "امبي سي 500 مجم 10*10 كبسول",
"scientificDrugId": "36",
"proxyDrugId": "01",
"prodCompDrugId": "06",
"pack": "10x10Capsules",
"unit": "باكت",
"price": "2267",
"discount": "00",
"categoryId": "15",
"image": "http://icons.iconarchive.com/icons/graphicloads/medical-health/256/medicine-box-icon.png"
},
I need to replace each , which is before "drugName_en" with :{
Could this be done in Notepad++?
Thank you in advance.

This does the job, preserving the linebreak (whatever it is):
Ctrl+H
Find what: ,(\R)(?=\s+"drugName_en")
Replace with: :{$1
CHECK Match case
CHECK Wrap around
CHECK Regular expression
Replace all
Explanation:
, # a comma
(\R) # group 1, any kind of linebreak (i.e. \r, \n, \r\n)
(?= # positive lookahead, make sure we have after:
\s+ # 1 or more white spaces
"drugName_en" # literally
) # end lookahead
Replacement:
/{ # literally
$1 # content of group 1, the linebreak
Screen capture (before):
Screen capture (after):

this is not possible in notepad++ but it is possible in sublime text editor.i have tried it and it is completely work just try Sublime Text editor.
Download Sublime Text

Related

How to add character after a line

I'm trying to perform a few regex steps, and I'd like to add a quotation mark and a comma (",) at the end of these lines without altering any of the rest of the characters in the line.
How would I keep things intact but add the ", after the words: device1, device2, device3 ?
Example of lines I'm working with:
object network device1
host 192.168.1.11
object network device2
host 192.168.1.12
object network device 3
host 192.168.1.13
After my first step of regex, I have modified my first line to include the curly bracket and some formatting with the words "category" and "name" as shown below. However, I don't want to change the word device1, but want to include a quotation and comma after the word device1
{
"category": "network",
"name": "device1
host 192.168.1.11
{
"category": "network",
"name": "device2
host 192.168.1.11
{
"category": "network",
"name": "device3
host 192.168.1.13
I can't figure out how to include the ", with my first step in my regex replace sequence?
I'm using both regexr.com and Notepad++.
You can use this regex to match each entity in your input data:
object\s+(\w+)\s+([^\r\n]+)[\r\n]+host\s+([\d.]+)
This matches:
object\s+ : the word "object" followed by a number of spaces
(\w+) : some number of word (alphanumeric plus _) characters, captured in group 1
\s+ : a number of spaces
([^\r\n]+) : some number of non-end-of-line characters, captured in group 2
[\r\n]+ : some number of end-of-line characters
host\s+ : the word "host" followed by a number of spaces
([\d.]+) : some number of digit and period characters, captured in group 3
This can then be replaced by:
{\n "category": "$1",\n "name": "$2",\n "host": "$3"\n},
To give output (for your sample data) of:
{
"category": "network",
"name": "device1",
"host": "192.168.1.11"
},
{
"category": "network",
"name": "device2",
"host": "192.168.1.12"
},
{
"category": "network",
"name": "device 3",
"host": "192.168.1.13"
},
Regex demo on regex101
Now you can simply add [ at the beginning of the file and replace the last , with a ] to make a valid JSON file.
This is the regex "name": "(device\d+) but since you have not mentioned any programming language you might get some pattern error based on the language you are using for example "" in java will need escape character so if you are using java then use this regex
\"name\": \"(device\d+)
Now you have to extract group (device\d+) and put your " there
for example in java you can do it with string.replaceAll

How can I find the records which has last character "A" in notepad++

I have huge JSON file in notepad++. One my field is product. I want to find out all the products which has character A in last in Value.
This is my data
{
"ID": 689,
"product": "GIPA",
"JobID": 66349,
"FriendlyName": "Android",
},
{
"ID": 689,
"product": "TKNA",
"JobID": 66350,
"FriendlyName": "Android",
},
{
"ID": 689,
"product": "TNRG",
"JobID": 66351,
"FriendlyName": "Android",
},
{
"ID": 689,
"product": "GAJT",
"JobID": 66352,
"FriendlyName": " Android",
},
I have tried two way but those are not working
"product": "^[a-z|A-Z|0-9]+[^A]\s?I{1}$"
And
"product": ".*(\A)$"
How can I find first two records?
Note the major issues with your regexps:
"product": "^[a-z|A-Z|0-9]+[^A]\s?I{1}$" contains ^ inside the pattern and thus it will never match as there is no start of string in the middle of it, where there is no pattern matching a line break before ^
[a-z|A-Z] matches letters AND also |, do not use | in character classes if you do not mean to match a literal | char (it loses its "alternation" meaning in between [...]
[^A] matches any char but A
{1} is always redundant, remove it. All patterns inside an expression are tried once by default.
"product": ".*(\A)$" contains \A, start of string anchor, which also invalidates the pattern, it will no longer match any string
You can use
"product": "[^"]*A"
It matches
"product": " - a literal string
[^"]* - 0 or more chars other than "
A" - A" string.

vscode snippet - transform and replace filename

my filename is
some-fancy-ui.component.html
I want to use a vscode snippet to transform it to
SOME_FANCY_UI
So basically
apply upcase to each character
Replace all - with _
Remove .component.html
Currently I have
'${TM_FILENAME/(.)(-)(.)/${1:/upcase}${2:/_}${3:/upcase}/g}'
which gives me this
'SETUP-PRINTER-SERVER-LIST.COMPONENT.HTML'
The docs doesn't explain how to apply replace in combination with their transforms on regex groups.
If the chunks you need to upper are separated with - or . you may use
"Filename to UPPER_SNAKE_CASE": {
"prefix": "usc_",
"body": [
"${TM_FILENAME/\\.component\\.html$|(^|[-.])([^-.]+)/${1:+_}${2:/upcase}/g}"
],
"description": "Convert filename to UPPER_SNAKE_CASE dropping .component.html at the end"
}
You may check the regex workings here.
\.component\.html$ - matches .component.html at the end of the string
| - or
(^|[-.]) capture start of string or - / . into Group 1
([^-.]+) capture any 1+ chars other than - and . into Group 2.
The ${1:+_}${2:/upcase} replacement means:
${1:+ - if Group 1 is not empty,
_ - replace with _
} - end of the first group handling
${2:/upcase} - put the uppered Group 2 value back.
Here is a pretty simple alternation regex:
"upcaseSnake": {
"prefix": "rf1",
"body": [
"${TM_FILENAME_BASE/(\\..*)|(-)|(.)/${2:+_}${3:/upcase}/g}",
"${TM_FILENAME/(\\..*)|(-)|(.)/${2:+_}${3:/upcase}/g}"
],
"description": "upcase and snake the filename"
},
Either version works.
(\\..*)|(-)|(.) alternation of three capture groups is conceptually simple. The order of the groups is important, and it is also what makes the regex so simple.
(\\..*) everything after and including the first dot . in the filename goes into group 1 which will not be used in the transform.
(-) group 2, if there is a group 2, replace it with an underscore ${2:+_}.
(.) group 3, all other characters go into group 3 which will be upcased ${3:/upcase}.
See regex101 demo.

Notepad ++: how to remove all text before and after a string

I want to just keep the code for each line in this text, what is the regular expression for this
{"name": "Canada", "countryCd": "CA", "code": 393},
{"name": "Syria", "countryCd": "SR", "code": 3535},
{"name": "Germany", "countryCd": "GR", "code": 3213}
The expected result would be
CA
SR
GR
Kind of a hack (see #Totos comment) but works for your requirements:
.*"([A-Z]{2})".*
This needs to be replaced by $1, see a demo on regex101.com (side node: isn't Germany usually GER ?)
In notepad++ I would do a find and replace like:
.*?"countryCd": "([^"]+)".*
And replace that with:
\1
That way if for some reason your country code was not just 2 letters it would be captured correctly. The [^"] is a negative character class, meaning anything that isn't " and the + makes it at least 1 character. I find using negative character classes does what is actually intended.
And in this case you want to capture whatever is in the quotes after the country CD, and this will do the trick.

Extract folder and keep regex group order intact

Given te following strings:
/folder/subfolder/all
/folder/subfolder/all?a=b
/folder/anothersubfolder/all?a=b
/folder/all
/folder/all?a=b
/folder/anothersubfolder
/folder/anothersubfolder/all
/folder
The subfolder "all" is predefined and needs to be extracted seperatetly from the any other subfolder that may or may not exist in the string.
A regex like
^\/(folder)(\/[^/?]*)?(\/[^/?]*)?(\?.*)?$
does not work for me. The group containing the different folders should be fixed. With this regex the subfolder "all" is either in group 2 or 3.
The results of the regex should be something like:
Group 1: /folder (mandatory can only be "/folder")
Group 2: /subfolder (optional can be any string except "/all")
Group 3: /all (optional can only be "/all")
Group 4: ?a=b (optional any set of parameters)
^\/(folder)((?:\/(?!all)[^/?]*)?)((?:\/all)?)((?:\?.*)?)$
[["folder", "/subfolder", "/all", "" ],
["folder", "/subfolder", "/all", "?a=b"],
["folder", "/anothersubfolder", "/all", "?a=b"],
["folder", "", "/all", "" ],
["folder", "", "/all", "?a=b"],
["folder", "/anothersubfolder", "", "" ],
["folder", "/anothersubfolder", "/all", "" ],
["folder", "", "", "" ]]
There are two main tricks here:
Non-capturing groups ?: which tell the regex engine not to hold on to a match, but still use it for clumping regex parts together. It lets us do things like ((?:stuff)?), which makes a mandatory group that can be empty.
Negative lookahead ?! which tells the regex to NOT match a certain pattern. So in this case (?!all) says that "all" can't be in the second directory block. (note: this means the second directory can't start with "all")