multiple negative look around working in regex tester, failing in SwiftLint build - swiftlint

Here is a link to the regex tester.
It successfully matches the class Second { and the class MyClass {. In the tester, but when I bring the same rule into my swiftlint.yml it doesn't match the same classes in my project.
unnecessary_class:
name: "Class doesn't need to exist"
regex: 'class(?!(.*XC)|(.*UI)|(.*Model)|(.*model))'
message: "A new class should have a reason to exist, if it isn't a model, codable, UI subclass, nor a View conformer, then there are alternative approaches instead of creating a new class."
severity: error

Related

Removing sensitive informations from the logs using regex

In my Ruby app I have the following regex that helps me with removing sensitive informations from logs:
/(\\"|")secure[^:]+:\s*\1.*?\1/
It works when in logs are the following information:
{"secure_data": "Test"}
but when instead of string I have object in logs it does not work:
{"secure_data": {"name": "Test"}}
How can I update regex to work with both scenarios?
https://rubular.com/r/h9EBZot1e7NUkS
You may use this regex with negated character classes and an alternation:
"secure[^:]+:\s*(?:"[^"]*"|{[^}]*})
Inside non-capturing group (?:"[^"]*"|{[^}]*}) we are matching a quoted string or an object that starts with { and ends with }.
Update RegEx Demo
The following should work for what you're trying to do. I'd suggest using a json parser though.
{"secure[^:]*?:\s({?(?:(?:,[^"]*?)?"[^"]*?"(?::\s"[^"]*?")?)*?)*?}?}
With this regex the object in secure_data may also contain multiple key-value(string)-pairs. It will still match. Other objects will not.

Add interface to all classes with a certain naming pattern

I am looking to refactor a large number of classes.
All the classes follow the naming convention
class SomethingModel
I want to find them all and replace them with
class SomethingModel : IModel
But I also want to exclude some template derives I have that look like
class SomethingController : GenController<SomethingModel>
I am trying to do this in Visual Studio.
The obvious choice is regex Find and Replace but I also have Resharper and Ideally I would do it using the pattern search replace
Through some searching on regex questions I have got the first part working
(?<=class)(.*Model)
but I cant seem to exclude the template. I am starting to play with
(class .*Model)(?!"(?<="Generic"))
I very rarely use regex and if there is a none-regex solution I would much prefer it. In particular I would quite like to use Resharper Search with Pattern
Which I tried the following with:
Which worked really well....apart from all my classes became empty... the statements tag does not seem to work (set to inifinite)
UPDATE
Positive cases:
public class DoorModel
public class HandleModel
public class BellModel
Negative cases
public class DoorController : GenericController
public class WindowModel : IModel
There is a hacky solution if you do wish to leverage Resharper. But its a hack.
According to Jetbrains the pattern search and replace is designed for functions, which is why it doesn't handle my class example above very well (see my conversation here )
What I did was open all the files I wanted to edit and did a find replace to replace to get resharper to ignore my class contents.
I wanted my resharper pattern replace so that I could replace this pattern:
class $ClassName$ : IModel
{
}
with this one
class $ClassName$ : AbstractModel<$ClassName$s>{}
So I simply did a find replace that replaced "IModel" with "IModel{}//$$"
Then ran the resharper pattern replace, which then worked because it obviously didnt "see" the contents of the class anymore.
Then I just replaced ">{}//$$" with ">" (hence the extra crazy symbols).
So yer its very hacky, but it does work. Thankfully its very rare you want to do crazy stuff like this (and should generally avoid it all costs). I actually discovered a few other "Resharper hacks" while messing around with this code which I might have to post about later.
You could try to match
(?<=class\s)(\w+Model)(?!\s+:\s*(GenericController|IModel))
and replace using the first capturing group like this
\1 : IModel
as seen in https://regex101.com/r/aMaPwF/4

In actionscript, how to check if a given regular expression is compiled or not using RegExp class

I'm new to actionscript, working on avm2..
One thing that I want to know is how to determine if a given regular expression is compiled or not in RegExp Class which use pcre library internally as regular expression compiler.
For example, the following has parenthesis-unmatched regular expression, which may be not compiled in pcre in RegExp class.
var r:RegExp = new RegExp("(a))");
I tried to use try-catch like the following, no exception occured.
try
{
var r:RegExp = new RegExp("(a))");
} catch (e:Error) {
trace('error');
}
I also tried to find solution on the Internet, there seem to be no method or properties for it.
Thanks.
According to the documentation, an error in the regular expression will throw a SyntaxError:
A SyntaxError exception is thrown when a parsing error occurs, for one of the following reasons:.
• An invalid regular expression is parsed by the RegExp class.
• Invalid XML content is parsed by the XML class.
As a subclass of Error, your code should have caught it, but it appears broken in my testing.

Find string matching pattern1 but not matching pattern 2

I am trying to work on a piece of code that will refactor my system.
So I will read all my classes and find every class or object that is called Manager using regex.
I want to do that only for the classes I wrote, so I do not want to find BeanManager and EntityManager classes.
Currently my regex is
/([a-zA-Z]*)Manager/
This works nice, but BeanManager and EntityManager are also included.
I've found this kind of question: Regular expression to match a line that doesn't contain a word?. In this case the OP wanted to find anything that doesn't match a pattern, but in my case I would like to find everything matching a pattern except if it matches a second pattern
Is there any way I can do that?
Sorry, I forgot the examples
I would like to include things like
MyManager
myManager
ClientManager
clientManager
testManager
TestManager
but exclude
BeanManager
EntityManager
Use word boundaries with a negative look-ahead to exclude the compounds that you want to ignore:
\b(?!Bean|Entity)([a-zA-Z]*)Manager\b
See demo

Find replace named groups regexp in Geany

I am trying to replace public methods to protected methods for methods that have a comment.
This because I am using phpunit to test some of those methods, but they really don't need to be public, so I'd like to switch them on the production server and switch back when testing.
Here is the method declaration:
public function extractFile($fileName){ //TODO: change to protected
This is the regexp:
(?<ws>^\s+)(?<pb>public)(?<fn>[^/\n]+)(?<cm>//TODO: change to protected)
If I replace it with:
\1protected\3\//TODO: change back to public for testing
It seems to be working, but what I cannot get to work is naming the replace with. I have to use \1 to get the first group. Why name the groups if you can't access them in the replacing texts? I tried things like <ws>, $ws, $ws, but that doesn't work.
What is the replacing text if I want to replace \1 with the <ws> named group?
The ?<ws> named group syntax is the same as that used by .NET/Perl. For those regex engines the replacement string reference for the named group is ${ws}. This means your replacement string would be:
${ws}protected${fn}\//TODO: change back to public for testing
The \k<ws> reference mentioned by m.buettner is only used for backreferences in the actual regex.
Extra Information:
It seems like Geany also allows use of Python style named groups:
?P<ws> is the capturing syntax
\g<ws> is the replacement string syntax
(?P=ws) is the regex backreference syntax
EDIT:
It looks my hope for a solution didn't pan out. From the manual,
A subpattern can be named in one of three ways: (?...) or (?'name'...) as in Perl, or (?P...) as in Python. References to capturing parentheses from other parts of the pattern, such as backreferences, recursion, and conditions, can be made by name as well as by number.
And further down:
Back references to named subpatterns use the Perl syntax \k or \k'name' or the Python syntax (?P=name).
and
A subpattern that is referenced by name may appear in the pattern before or after the reference.
So, my inference of the syntax for using named groups was correct. Unfortunately, they can only be used in the matching pattern. That answers your question "Why name groups...?".
How stupid is this? If you go to all the trouble to implement named groups and their usage in the matching pattern, why not also implement usage in the replacement string?