RegEx - How to find all App\Model and not App\Something\Model - regex

Upgrading from Laravel 6 to 8. I want to replace all App\Models to App\Models\Model and of course I don't want to replace App\Something\Class nor App\Something\SomethingElse\Class. How can I achieve this using RegEx?

If you're just looking to replace references and don't need to move the files, then the following regex should work (note I am no regex wizard so there is likely a better method).
^App\\([\w]+;)$
You can then use the capture groups () to replace and insert your new pathing:
App\Models\\$1

Related

Regex find a specific character zero or one or multiple time in a string

I'm upgrading a Symfony app with VSCode and I have numerous occurences of this kind of string :
#Template("Area:Local:delete.html.twig")
or
#Template("Group:add.html.twig")
In this case, I want to replace all the : with / to have :
#Template("Area/Local/delete.html.twig")
I can't think of doing it manually, so I was looking for a regular expression for a search/replace in the editor.
I've been toying with this fearsome beast without luck (i'm really dumb in regexp) :
#Template\("([:]*)"
#Template\("(.*?)"
#Template\("[a-zA-Z.-]{0,}[:]")
Still, I think there should be a "simple" regexp for this kind of standard replacement.
Anyone has any clue ? Thank you for any hint
You can use this regex with a capture group: (#Template.*):.
And replace with this $1/.
But you'll have to use replace all until there's no : left, that won't take long.
Just explaining a lit bit more, everything between the parenthesis is a capture group that we can reference later in replace field, if we had (tem)(pla)te, $1 would be tem and $2 would be pla
Regex!
You can use this regex #Template\("(.[^\(\:]*)?(?:\:)(.[^\(\:]*)?(?:\:)?(.[^\(\:]*)?"\) and replacement would simply be #Template\("$1/$2/$3
You can test it out at https://regex101.com/r/VfZHFa/2
Explanation: The linked site will give a better explanation than I can write here, and has test cases you can use.

Search and replace with particular phrase

I need a help with mass search and replace using regex.
I have a longer strings where I need to look for any number and particular string - e.g. 321BS and I need to replace just the text string that I was looking for. So I need to look for BS in "gf test test2 321BS test" (the pattern is always the same just the position differs) and change just BS.
Can you please help me to find particular regex for this?
Update: I need t keep the number and change just the text string. I will be doing this notepad++. However I need a general funcion for this if possible. I am a rookie in regex. Moreover, is it possible to do it in Trados SDL Studio? Or how am i able to do it in excel file in bulk?
Thank you very much!
Your question is a bit vague, however, as I understand it you want to match any digits followed by BS, ie 123BS. You want to keep 123 but replace BS?
Regex: (\d+)BS matches 123BS
In notepad++ you can:
match (\d+)BS
replace \1NEWTEXT
This will replace 123BS with 123NEWTXT.
\1 will substitue the capture group (\d+). (which matches 1 or more digits.
You could do this in Trados Studio using an app. The SDLXLIFF Toolkit may be the most appropriate for you. The advantage over Notepad++ is that it's controlled and will only affect the translatable text and not anything that might break the integrity of the file if you make a mistake. You can also handle multiple files, or even multiple Trados Studio projects in one go.
The syntax would be very similar to the suggestion above... you would:
match (\d+)BS
replace $1NEWTEXT

VS 2017 find and replace ConfigurationManager.Appsettings["stringname"]

I am trying to find and replace ConfigurationManager.Appsettings["stringname"] and Convert.ToInt32(ConfigurationManager.Appsettings["stringname"]) (where "stringname" could be any valid AppSetting name) in all the files in my project with a call to a custom method which wraps this.
There are around 1000 of these entries in the project, trying to avoid doing this manually on each file.
Is there a better way to do this? Gave a few tries with the Find in all files with a regex, but with no luck.
Thanks in advance.
You should be able to match and replace all with this regex:
ConfigurationManager.Appsettings\[([^\])\]
The regex search for the string 'ConfigurationManager.Appsettings[', then creates a capturing Group containing the content of the Square brackets.
You then need to replace with:
ClassA.Method($1)
That will replace with the string, where '$1' will be replaced by the matched Group 1 from the regex (which would be for instance '"SessionTimeout"').

Find and replace with regular expressions in Intellij

I got the following string:
http://www.foo.com/images/bar/something/image.gif|png
I would like to replace all the occurences of such string as:
<someTag>/images/bar/something/image.gif|png</someTag>
How can I achieve this using a regEx?
To open Replace in Path popup press Ctrl+Shift+R (or on mac Cmd+Shift+R).
If you want to switch everywhere in your project, make sure the scope is In Project.
Make sure Regex? is checked and put the next values in the boxes:
(http://)([a-zA-Z.0-9]*)([a-zA-Z.-/0-9#?%&|]*)
<someTag>$3</someTag>
You will need to make sure all the characters you need are in the expression. For example strings with # will not be replaced as expected.
The way this works is the patterns matched are matched in 3 groups and by using the parenthesis and then only the 3d group is being used to replace the string found.
Example:
Good luck!

How to add stripslashes to get_option()

Hy, I created a WordPress Theme using a lot of custom options. While saving the options in the backend amongst other things I'm adding backslashes before characters that need to be escaped. Such as ' and ".
Now I need to remove them before displaying them on the frontend. The easiest way would be stripslashes(get_option($key)). But that would mean I'd have to go though the whole Theme and change all get_option() manually.
Is there a way to add a filter to the get_option()?
If not, is there a way to achieve this with find/replace (I'm using Sublime Text 3 which allows regex)?
Why not just create your own function in place of get_option() (but which takes advantage of it)? For example, you could define the following in functions.php:
function my_stripslashes_function($option, $default = false) {
return stripslashes( get_option($option, $default) );
}
And then use Sublime to replace all instances of get_option with my_stripslashes_function...no regex required, and it's more DRY.
Yes in Sublime Text you can realize it with an regex:
find: get_option\((.[^)]*)\)
replace with: stripslashes(get_option($1))
If you need to debug a regex yourself here is a helpful tool: https://regex101.com/r/rY7oG6/2