Visual Studio Regex Find and Replace function - c#

I'm trying to find all var.AppendLine("..."); and replace them with Append("...\n");
Been fooling around with regex's but don't seem to get anywhere. Anyone has a suggestion on what regular expression to use here?
var can be a variable name and I need to select the ... for replace with Append("$1\n");

I assume you actually don't want to get rid of var:
Search: <{[a-zA-Z0-9]+}.AppendLine\("{[^"]+}"\)
Replace with: \1.Append("\2\\n")

I think you meand the regex in the "search& replace window of VS ? Then something like
<{[a-zA_Z]+\.}{AppendLine\("}{[^"]+}{"\)}
to replace with
\1Append("\3\\n")
(remove the \1 if you want to remove the "var." part, not clear in your question)

Related

Replace beginning and end of string with unique midle?

I have lots of code like below:
PlusEnvironment.EnumToBool(Row["block_friends"].ToString())
I need to convert them to something like this.
Row["block_friends"].ToString() == "1"
The value that gets passed to EnumToBool is always unique, meaning there is no guarantee that itll be passed by a row, it could be passed by a variable, or even a method that returns a string.
I've tried doing this with regex, but its sort of sketchy and doesn't work 100%.
PlusEnvironment\.EnumToBool\((.*)\)
I need to do this in Visual Studio's find and replace. I'm using VS 17.
If you had a few places where PlusEnvironment.EnumToBool() was called, I would have done the same thing that #IanMercer suggested: just replace PlusEnvironment.EnumToBool( with empty string and the fix all the syntax errors.
#IanMercer has also given you a link to super cool, advanced regex usage that will help you.
But if you are skeptical about using such a complex regex on hundreds of files, here is what I would have done:
Define my own PlusEnvironment class with EnumToBool functionality in my own namespace. And then just replace the using Plus; line with using <my own namespace>; in those hundreds of files. That way my changes will be limited to only the using... line, 1 line per file, and it will be simple find and replace, no regex needed.
(Note: I'm assuming that you don't want to use PlusEnvironment, or the complete library and hence you want to do this type of replacement.)
in Find and Replace Window:
Find:
PlusEnvironment\.EnumToBool\((.*))
Replace:
$1 == "1"
Make sure "Use Regular Expressions" is selected

Too short control escape. How to get Regex for this?

So, let's say I have a result from a search that comes back as:
\\my.test.site#SSL\JohnDoe\SusanSmith\courses\PDFs\Science_Math\BIOL\S12014 Syllabi\BIOL-1322-S12014-John-Doe.pdf
Whenever the result is listed in a text box I get the entire path instead of just the file. This is functioning as designed since I can't use the .Select(Path.GetFileName) while enumerating directories lest it doesn't have the full path to do the search on.
So, I was going to use Regex to do a replace at the end when the results are displayed however when I went to Rubular it doesn't like either my expression or the test string(can't figure out which).
I basically want to cut down everything except the file name and extension.
So my Regex was supposed to be something like:
\\my.test.site#SSL\JohnDoe\SusanSmith\courses\PDFs\.+\.+\.+\
So that I get everything up to the file name and extension for deletion. However Rubular doesn't like something as I get a "too short control escape" error. I don't want to test this in C# without verifying in Rubular since I use it heavily and figure if it won't work there it won't work at runtime.
Any ideas? Thanks.
Remember to escape the \ characters, as well as the literal . characters:
\\\\my\.test\.site#SSL\\JohnDoe\\SusanSmith\\courses\\PDFs\\.+\\.+\\.+\\
Also note, you probably want to avoid over-matching on the .+ by using non-greedy quantifiers:
\\\\my\.test\.site#SSL\\JohnDoe\\SusanSmith\\courses\\PDFs\\.+?\\.+?\\.+?\\
Or using character classes:
\\\\my\.test\.site#SSL\\JohnDoe\\SusanSmith\\courses\\PDFs\\[^\\]+\\[^\\]+\\[^\\]+\\
Maybe I'm misinterpreting the question, but it sounds like your approach has been overly complicated.
Can't you simply match this: .+\\
And then replace with '' (nothing)?

Issue regarding Regular Expression

I want to match all "the act" outside the tags in the *.sgm that my professor gave me, I know that we can use XML parser, but our goal is to learn REGEX purely.
this is my current Regex:
(?<![""=<\/])\bthe act\b(?!\>)
The problem is with this example:
<ptext>Test example the act example</ptext>
My regex matches "the act". And that is correct.
But if this example now I will try:
<ptext tags="Test the act">Example the act</ptext>
The regex will match (2) two "the act", the one that is inside the tag attribute and the one outside, I dont want to match all the act inside the tag, how can I do that? thanks.
Maybe this will work: (?<=\>[^>]*)the act(?=[^<]*\<) It should work if the regex engine allows variable length look behind, I think c#'s engine does.

Regex to check whether "and,or,not,and not" in a word?

I have a seneario where i have to check a word contains "and,or,not,and not" but the regex which i have created fails. Can any body provide me the correct regex for this?
The regex which i have created is like this
Regex objAlphaPattern = new Regex(#"^[not|and not|and|not]");
if(objAlphaPattern.IsMatch(searchTerm))
{
//// code
}
But it always returns true.
I have tried the word "Pen and Pencil" and "Pen Pencil" but both returning true.. Can anybody help in providing correct regex?
You're starting with a begin anchor. If you don't want to only check if it happens at the beginning of the string then you shouldn't have the ^.
Also, you are using [] when you should be using (). Actually in this case you don't even need ().
[] indicates a character class. You just don't need that.
Regex objAlphaPattern = new Regex("\b(and|not)\b");
if(objAlphaPattern.IsMatch(searchTerm))
{
//// code
}
That should do the job.
I highly recommend The Regex Coach to help you build regex.
I also highly recommend http://www.regular-expressions.info/ as a reference.
EDIT:
I feel I should point out you don't really even need the object instance.
if(System.Text.RegularExpressions.Regex.IsMatch(searchTerm, "\b(and|not)\b"))
{
//// code
}
You can just use the static method.
That's a very good point Tim:
"\band\b|\bnot\b"
Another very good point stema:
"\b(and|not)\b"
try
(not)|(and not)|(and)
instead
Your regular expression is wrong, it should be (and|not). There is no need to check for and not either, since it will fail at the first and.
You can use an online tool to check your regular expressions; such as http://regexpal.com/?flags=&regex=(and|not)&input=Pen%20and%20Pencil

How do I fix this regular expression?

I have the string:
CN=Help & Technical,CN=Users,DC=dave,DC=com
And I want to strip out everything between the '=' and the ',' in a set of groups. Basically Im using this...
=([\w-\s]*)
And it is only dragging back the following :
=help
=users
=dave
So you can see im not getting Help & Technical in the first group which is what I want. Is this possible can anybody help me with the regex I just cant work it out...
I haven't tested this, but =([^,]*) should work.
You just need to include the & sign in your regular expression here.
=([\w-\s&]*)
Note that this is pretty restrictive so far... no apostrophes, no numbers, and no other punctuation. You may want to consider whether any of that will show up and add them as appropriate.
This should work
=(.+),|\w
It should match everything after the = until a , or a enter

Categories