regex search in vs2010 - regex

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).

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

Preserve case during visual studio regex find and replace

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.

Visual Studio 2010: pattern based search replace other than regex?

I've taken over a project that is full of code like this:
if (aTraceUserids[t].Trim().ToUpper() == Userid().Trim().ToUpper())
{
// ...
}
What is - using tool-assisted expression formulation - a good way to do a search replace into something like this on a case by case base:
if (aTraceUserids[t].Equals(Userid(), StringComparison.InvariantCultureIgnoreCase))
{
// ...
}
Edit (thanks Dave for making me think on this further):
I know this should be possible with regular expressions, but those are hard to get right and document, so I wonder about tool assisted ways that help me both phrase the expressions and execute them.
Ideally I'm looking for a pattern based search/replace tool that allows me to
enter the search/replace patterns
enter the patterns for the files and directory names to match
visually assists me with the search/replace matches, and allows me to post-edit each occurrence
I don't care much which platform as these kinds of search/replace actions will likely apply to other big code bases as well.
So: any solution based on *nix, Windows or web are fine. CygWin and/or WINE based solutions are fine too. (That's why I removed the VS2010 tag and added some platform tags).
Since this was originally tagged with 'Visual Studio', Visual Studio itself can do regular-expression based find/replace, and the standard 'Find and Replace' dialog will let you pick and choose by hitting 'Find Next', 'Replace' or 'Replace All' as you choose.
For example, I recently changed an API from;
Log.Error(string message, bool someotherArg);
to
Log.Error(string message);
And easily used Visual Studio to replace all usage throughout my codebase of this modified API like so;
Find what;
Log.Error({.*}, true);
Replace with:
Log.Error(\1);
The backquote in the replace line \1 puts the grouped regex contained by {...} into that spot in the replace.
Handy, and built-in. Works for me.

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)