Preserve case during visual studio regex find and replace - regex

I'm trying to find and replace strings using the Visual Studio regex find and replace in some code which includes a lot of inline documentation.
e.g. replace "east" with "north", and "East" with "North".
Since the files contain grammatically correct English right now, I want to be careful not to alter the case of text that may get replaced in the comments.
I know you can turn on the match case, or have one regex for lowercase and one for capitalized words, but I'm wondering if I actually have to do it twice or not (obviously I don't want to).
I've seen other answers for perl and javascript which give language-specfic answers to this question (requiring callbacks), but I'm wondering if it's possible to do just within the visual studio dialog.

If you study Using Regular Expressions in Visual Studio, you will see that there are no such an operator that would keep the case of any specified letter matched/captured with a regex.
In some regex flavors, like in Perl and R (g)sub, you could turn your captures/matches lower/uppercase with a specific operator, but again, it would be a hardcoded action, not keeping the original case intact.
Thus, the only option you have with regex is to run individual search and replace operations (like east --> north and East --> North, maybe with word boundaries around \beast\b to match a whole word).
Else, you need to process the text with some custom code written in some full fledged language.

Related

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

REGEX in MS Word 2016: Exclude a simple String from Search

So I read a lot about Negation in Regex but can't solve my problem in MS Word 2016.
How do I exclude a String, Word, Number(s) from being found?
Example:
<[A-Z]{2}[A-Z0-9]{9;11}> to search a String like XY123BBT22223
But how to exclude for example a specefic one like SEDWS12WW04?
Well it depends on what you need to achieve or is this a matter of curiosity... RegEx is not the same as the built-in Advanced Find with Wildcards; for that you need VBA.
Depending on your need, without using VBA, you could make use of space and return characters - something like this will work for the strings provided: [ ^13][A-Z]{2}[0-9]{1,}[A-Z]{1,}[0-9]{1,}[ ^13] (assuming you use normal carriage returns and spaces in your document)
Anyway, this is a good article on wildcard searches in MS Word: https://wordmvp.com/FAQs/General/UsingWildcards.htm
EDIT:
In light of your further comments you will probably want to look at section 8 of the linked article which explains grouping. For my proposed search you can use this to your advantage by creating 3 groups in your 'find' and only modifying the middle group, if indeed you do intend to modify. Using groups the search would look something like:
([ ^13])([A-Z]{2}[0-9]{1,}[A-Z]{1,}[0-9]{1,})([ ^13])
and the replace might look like this:
\1 SOMETHING \3
Note also: compared to a RegEx solution my suggestion is kinda lame, mainly because compared to RegEx, MS-Words find and replace (good as it is, and really it is) is kinda lame... it's hacky but it might work for you (although you might need to do a few searches).
BUT... if it really is REGEX that you want, well you can get access to this via VBA: How to Use/Enable (RegExp object) Regular Expression using VBA (MACRO) in word
And... then you will be able to use proper RegEx for find and replace, well almost - I'm under the impression that the VBA RegEx still has some quirks...
As already noted by others, this is not possible in Microsoft Word's flavor of regular expressions.
Instead, you should use standard regular expressions. It is actually possible to use standard regular expressions in MS Word if you use a special tool that integrates into Microsoft Word called Multiple Find & Replace (see http://www.translatortools.net/products/transtoolsplus/word-multiplefindreplace). This tool opens as a pane to the right of the document window and works just like the Advanced Find & Replace dialog. However, in addition to Word's existing search functionality, it can use the standard regular expressions syntax to search and replace any text within a Word document.
In your particular case, I would use this:
\b[A-Z]{2}[A-Z0-9]{9,11}\b(?<!\bSEDWS12WW04)
To explain, this searches for a word boundary + ID + word boundary, and then it looks back to make sure that the preceding string does not match [word boundary + excluded ID]. In a similar vein, you can do something like
(?<!\bSEDWS12WW04|\bSEDWS12WW05|\bSEDWS12WW05)
to exlude several IDs.
Multiple Find & Replace is quite powerful: you can add any number of expressions (either using regular expressions or using Word's standard search syntax) to a list and then search the document for all of them, replace everything, display all matches in a list and replace only specific matches, and a few more things.
I created this tool for translators and editors, but it is great for any advanced search/replace operations in Word, and I am sure you will find it very useful.
Best regards, Stanislav

regex search in vs2010

How can I perform a case insensitive search that will find two words that may or may not have a space between them in Visual Studio 2010 e.g.
Foo Bar
FooBar
fooBar
foo bar
EDIT:
Sorry I should have been clearer - I mean a ctrl+shift+f (Edit -> Find and Replace -> Find in Files) search in VS2010 - I don't want to implement this in code - I want to find code/comments etc that matches the search criteria above.
I could also just do four searches... but I am interested to know how to do this in one go, and I would feel more comfortable doing it in one go as I am refactoring.
Cheers
Rob
Use the regex:
foo:b*bar
In VS search form:
In code Regex has a case sensitivity option.
You could do it with a single regular expression, but I believe the code will be more readable if you handle each case seprately. Check if the string looks like "FooBar" (maybe using a regex), and if it doesn't, just count the number of spaces in the string (you can use Trim()) to eliminate the leading and training spaces).

Replacing all instances of a name in all strings in a solution

We have a large solution with many projects in it, and throughout the project in forms, messages, etc we have a reference to a company name. For years this company name has been the same, so it wasn't planned for it to change, but now it has.
The application is specific to one state in the US, so localizations/string resource files were never considered or used.
A quick Find All instances of the word pulled up 1309 lines, but we only need to change lines that actually end up being displayed to the user (button text, message text, etc).
Code can be refactored later to make it more readable when we have time to ensure nothing breaks, but for time being we're attempting to find all visible instances and replace them.
Is there any way to easily find these "instances"? Perhaps a type of Regex that can be used in the Find All functionality in Visual Studio to only pull out the word when it's wrapped inside quotes?
Before I go down the rabbit hole of trying to make my job easier and spending far more time than it would have taken to just go line by line, figured I would see if anyone has done something like this before and has a solution.
You can give this a try. (I hope your code is under source control!)
Foobar{[^"]*"([^"]*"[^"]*")*[^"]*}$
And replace with
NewFoobar\1
Explanation
Foobar the name you are searching for
[^"]*" a workaround for the missing non greedy modifier. [^"] means match anything but " that means this matches anything till the first ".
([^"]*"[^"]*")* To ensure that you are matching only inside quotes. This ensures that there are only complete sets of quotes following.
[^"]* ensures that there is no quote anymore till the end of the line $
{} the curly braces buts all this stuff following your companies name into a capturing group, you can refer to it using \1
The VS regex capability is quite stripped down. It perhaps represents 20% of what can be done with full-powered regular expressions. It won't be sufficient for your needs. For example, one way to solve this quote-delimited problem is to use non-greedy matching, which VS regex does not support.
If I were in your shoes, I would write a perl script or a C# assembly that runs outside of Visual Studio, and simply races through all files (having a particular file extension) and fixes everything. Then reload into Visual Studio, and you are done. Well, if all went well with the regex anway.
Ultimately what you really must watch out for is code like this:
Log.WriteLine("Hello " + m_CompanyName + " There");
In this case, regex will think that "m_CompanyName" appears between two quotes - but it is not what you meant. In this case you need even more sophistication, and I think you'll find the answer with a special .net regular expression extension.

search in visual studio using regexp

I have a situation where i need to search for couple of objects in my code files. currently i m doing it by seaching using visual studio search option for every text i want to search.
I want to use regular expression ( search -> use -> regular expression ) to search all my text at once using OR operator.
Please suggest me for that, as i am not much familiar with regular expression syntax.
Sry for editing in question itself..
I got the answer. Like if I want to search for objects 'abc','xyz' I would put abc|xyz in visual studio seach box. But i don't know how to make this search case insensitive. I got a hint of using /i or -i or ?i , but where and how - i don't know .
As far as I know, Visual Studio should search case insensitive, unless you check the box that says "Match case" (see screenshot).
You can use the alternation operator | to effectively OR part of the regex. So (foo)|(bar) will find either the text "foo" or the text "bar". Either side, can of course, be a regular expression on its own, so you can come up with some pretty complicated stuff.
But as zzzzBov said, if you want any more help you're going to have to supply more information. Or you could actually, you know, read the documentation.
I've found that the search in file using regular expression is buggy in regard to case sensitivity in Visual Studio 2015. Even with the "Match case" option turned on, it will match text ignoring case by default.
The one trick that somehow fixed this is by using capture, surrounding the literal text you want with parenthesis in addition to the "Match case" option!
So instead of: abc.def
Use: (abc).(def)