简体   繁体   中英

Find and replace with regular expression in Notepad++

At the moment, I have a PHP function that gets the contents of a CSV file and puts it into a multi-dimensional array, which contains text that I print out in various places, using the indexes.

an example of use would be:

$localText[index][pageText][conceptQualityText][$lang];

The first index, [index] , would be the name of the page. The second index [pageText] would indicate what it is (text for the page). The third index, [conceptQualityText] indicates what the actual text is. The last index, [$lang] gets the text in the desired language.

so:

->page location

->what is it

->the content

->what language it should be displayed in.

This all worked fine in the previous PHP versions. However, upgrading to 7.2, PHP seems to be a bit more strict. I was a bit more green ~2 years ago when I first made this solution, and now know that since these indexes aren't defined as strings eg encapsulated in single quotes like so: ['index'] , they fit the notation of a superglobal (DEFINE). I didn't give it much thought back then, but now PHP seems to interpret them as so (superglobals), and so I get thrown the error that x word is an undefined superglobal.

My initial thought is to make a search and replace on my example string:

$localText[index][pageText][conceptQualityText][$lang];

using the regular expression functionality in Notepad++.

However, the example is just one of many, the notation of the array indexing is basically:

$localText[index][index2][index3][$lang];

So my question is:

How can I make use of the Notepad++ search and replace, using a regular expression, so that my index pointers become strings, instead of acting as superglobal variables?

eg make:

$localText[index][index2][index3][$lang];

into:

$localText['index']['index2']['index3'][$lang];

I will need some sort of logic that checks for whatever is inside the brackets and encapsulates them with single quotes, except for the last index, [$lang] .

I tried to give as much information as possible, let me know if anything needs to be elaborated.

I tried to refer to these docs without much luck.

You can use the following regex to match:

\[[^'](\w+)[^']\]

The regex matches a Word between Square brackets unless it quoted.

Replace with:

['$1']

The regex will not match the last brackets because it contains a ' $ ' sign.

I found a solution using

this:

find: \\b(localText\\[)([a-zA-z0-9_\\-]+)(\\]\\[)([a-zA-z0-9_\\-]+)(\\]\\[)([a-zA-z0-9_\\-]+)

replace: $1'$2'$3'$4'$5'$6'

and it works like a charm. Thanks for everyone who took their time to help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM