Disable #imageLiteral(resourceName: <..>) preview in Xcode? - xcode

I found image literals to be rather distracting than useful.
Is there any way to disable this Xcode feature?

A good method for this is to replace all occurrences of #imageLiteral with UIImage(imageLiteralResourceName:) initializers (thanks for the suggestion, #D6mi!). Here's how you can do it automatically:
Navigate to Find/Find and Replace... (or press ⌥⌘F).
Open the dropdown list on the right side and select Regular Expression.
For the search term, enter the following regex:
#imageLiteral\(resourceName: (.*)\)
For the replacement, enter this:
UIImage(imageLiteralResourceName: $1)
This regular expression captures the value of the resource name with (.*) and inserts it again with $1. The backslashes are for escaping the parentheses, since they count as special characters.
Note that you don't have to use regular expression in this case (as LinusGeffarth pointed out), but it can be more useful in more complex cases than this.

Related

Minus sign that appears not to designate a range and escaping a closing parenthesis

I have 2 questions regarding the following regex from Why's Poignant Guide to Ruby:
1: What does the minus sign mean here? It doesn't seem to be designating a range because there is nothing to the left of it other than the bracket.
2: Why is it necessary to escape the closing parenthesis? After you escape the opening one, what special meaning could the closing parenthesis have?
/\([-\w]+\)/
1)When the minus sign is at the begining or at the end of a character class, it is seen as literal.
2) escaping closing parenthesis is a convention. The goal is IMO, to avoid an ambiguity with a possible opening parenthesis before. Consider these examples:
/(\([-\w]+\))/ or /(\([-\w]+)\)/
1) The minus sign is a literal minus sign. Since it cannot possibly designate a range, it has no special meaning and so the character class is equivalent to [\-\w] - escaping the hyphen is optional, as you observe in your second point...
2) ...however, it isn't always good form to not escape something just because the regular expression engine allows it. For example, this regex: ([([^)-]+) is perfectly valid (I think...) but entirely unclear because of the fact that characters which normally have special meanings are used as literal characters without being escaped. Valid, yes, but not obvious, and someone who doesn't know all the rules will become very confused trying to understand it.
The the minus sign -, or say the hyphen, means exact just the character -. The hyphen can be included right after the opening bracket, or right before the closing bracket, or right after the negating caret. It's not designating a range, so it's not confusing. You can also choose to use \- if you like.
As to why to escape ), I think it means to reduce the regex engine's work so that it doesn't have to remember if an opening parenthesis is before.
- sign in this regex actually means a - sign that you want to see in the text.
Non-escaped parentheses means a match group, that will be available for you, for example, by $1 variable.
> "(-w)" =~ /\([-\w]+\)/
> $1 # => nil
and
> "(-w)" =~ /([-\w]+)/
> $1 # => -w
You can go to Rubular and try both regexes \([-\w]+\) and ([-\w]+) - and you will see different results by passing (-w) as a test. You can notice match groups appearing.

Visual Studio 'Find and Surround With' instead of 'Find and Replace'

So i want to surround all string literals in our C++ source with an _T(...) for our unicode port.
This questions answers how I search for string literals but is there some way of surrounding the matched text with _T() instead of replacing with something else?
I intend to do it one string at a time anyway and not all at once but want to avoid having to type it out or use "Surround With" from Visual Assist myself for each string.
Jochen Kalmbach's answer might work in older versions of Visual Studio, but it didn't work for me in Visual Studio 2013. However, the small RegEx shortcut buttons to the right of the Find/Replace input boxes helped a lot:
In Find, select the ":q Quoted string" option.
In Replace, select the "$1 Substitute the substring matched by captured group number 1", and then surround $1 with _T().
Final Output
Find:
((\".+?\")|('.+?'))
Replace:
_T($1)
Note that the $1 represents the RegEx expression group enclosed in the outermost parentheses.
Here's another example:
Requirement
Find:
Converter.toCustomObject($find("Anything"));
Replace (different Converter method and add parameter after $find() parameter):
Converter.toDifferentObject($find("Anything"), true);
Solution
Find (use RegEx in Find options):
Converter\.toCustomObject\((\$find\(.*)\);
Replace:
Converter.toDifferentObject($1, true);
Notice that the Replace value doesn't need to escape special characters, though you can apply some RegEx, e.g. to add a Line Break after the output, you can use this for Replace:
Converter.toDifferentObject($1, true);\r\n
Goto: Edit|Find and Replace...|Quick Replace..
Then enter:
Find: :q
Replace with : _T(\0)
Use: Regular Expressions

Custom code highlight Notepad++

I am creating a custom code highlight for notepad++. What I want to do is the following:
some fieldnames are writen in the code with a ' in front of their name, for exampe
if 'variable = "test" then ...
I would like to highlight these words, but notepad++ does not seem to allow a delimiter starting with ' and ending with a space, not does it allow space as an escape character. Also, using ' as a keyword and enabling prefix mode has no effect. Anyone has a suggestion? Should I use another expression to let notepad recognise the space/' ?
Thanks in advance!
If you only need to highlight a single word, you can use a keyword in prefix mode. However when using single or double quotes in a keyword, they need to be escaped with a backslash. So your keyword would be:
\'
This may not be possible in notepad++. I can get the behavior you want using a character other than a single quote, like a back-tic, but it doesn't seem to work with single or double quotes. I suspect those characters are treated special within the syntax highlighter.

Matching an unescaped balanced pair of delimiters

How can I match a balanced pair of delimiters not escaped by backslash (that is itself not escaped by a backslash) (without the need to consider nesting)? For example with backticks, I tried this, but the escaped backtick is not working as escaped.
regex = /(?!<\\)`(.*?)(?!<\\)`/
"hello `how\` are` you"
# => $1: "how\\"
# expected "how\\` are"
And the regex above does not consider a backslash that is escaped by a backslash and is in front of a backtick, but I would like to.
How does StackOverflow do this?
The purpose of this is not much complicated. I have documentation texts, which include the backtick notation for inline code just like StackOverflow, and I want to display that in an HTML file with the inline code decorated with some span material. There would be no nesting, but escaped backticks or escaped backslashes may appear anywhere.
Lookbehind is the first thing everyone thinks of for this kind of problem, but it's the wrong tool, even in flavors like .NET that support unrestricted lookbehinds. You can hack something up, but it's going to be ugly, even in .NET. Here's a better way:
`[^`\\]*(\\.[^`\\]*)*`
The first part starts from the opening delimiter and gobbles up anything that's not the delimiter or a backslash. If the next character is a backslash, it consumes that and the character following it, whatever it may be. It could be the delimiter character, another backslash, or anything else, it doesn't matter.
It repeats those steps as many times as necessary, and when neither [^`\\] nor \\. can match, the next character must be the closing delimiter. Or the end of the string, but I'm assuming the input is well formed. But if it's not well formed, this regex will fail very quickly. I mention that because of this other approach I see a lot:
`(?:[^`\\]+|\\.)*`
This works fine on well-formed input, but what happens if you remove the last backtick from your sample input?
"hello `how\` are you"
According to RegexBuddy, after encountering the first backtick, this regex performed 9,252 distinct operations (or steps) before it could give up and report failure; mine failed in ten steps.
EDIT To extract just the par inside the delimiters, wrap that part in a capturing group. You'll still have to remove the backslashes manually.
`([^`\\]*(?:\\.[^`\\]*)*)`
I also changed the other group to non-capturing, which I should have done from the start. I don't avoid capturing religiously, but if you are using them to capture stuff, any other groups you use should be non-capturing.
EDIT I think I've been reading too much into the question. On StackOverflow, if you want to include literal backticks in an inline-code segment or a comment, you use three backticks as the the delimiter, not just one. Since there's no need to escape backticks, you can ignore backslashes as well. Your regex could turn out to be as simple as this:
```(.*?)```
Dealing with the possibility of false delimiters, you use the same basic technique:
```([^`]*(?:`(?!``)[^`]*)*)```
Is this what you're after?
By the way, this answer doesn't contradict #nneonneo's comment above. This answer doesn't consider the context in which the match is taking place. Is it in the source code of a program or web page? If it is, did the match occur inside a comment or a string literal? How do I even know the first backtick I found wasn't escaped? Regexes don't know anything about the context in which they operate; that's what parsers are for.
If you don't need nesting, regexes can indeed be a proper tool. Lexers of programming languages, for instance, use regexes to tokenize strings, and strings usually allow their own delimiters as an escaped content. Anything more complicated than that will probably need a full-blown parser though.
The "general formula" is to match an escaped character (\\.) or any character that's valid as content but don't need to be escaped ([^{list of invalid chars}]). A "naïve" solution would be joining them with or (|), but for a more efficient variant see #AlanMoore's answer.
The complete example is shown below, in two variants: the first assumes than backslashes should only be used for escaping inside the string, the second assumes that a backslash anywhere in the text escapes the next character.
`((?:\\.|[^`\\])*)`
(?:\\.|[^`\\])*`((?:\\.|[^`\\])*)`
Working examples here and here. However, as #nneonneo commented (and I endorsed), regexes are not meant to do a complete parse, so you'd better keep things simple if you want them to work out right (do you want to find a token in the text, or do you want to delimit it already knowing where it starts? The answer to that question is important to decide which strategy works best for your case).

Wildcards to regex in VS find & replace

I need to convert expressions of the form:
return *;
into:
return filter(*);
It seems simple enough to express it with wildcards, however, in visual studio's search & replace dailog, there's no way to associate the first asterisk with the second one. I suppose a regex can do this quite easily, however I know very little about regexes.
How do I express this criteria in regex?
A capture group when searching/replacing with regex in VS can be given by enclosing something with curly braces.
A backreference can be given simply by using \1. There is also a menu to the right of the input fields, containing building blocks.
So you would be simply replacing
return {[^;]+};
by
return filter(\1);
The [^;]+ specifies that you want at least one character that is not a semicolon, so unless you return delegates or anonymous methods this should work fine.

Resources