Below I have a regex statement I have been working on for quite awhile. The problem I am having is that their are a lot of quotations I am trying to parse out (I think that's the terminology I am looking for) so Visual Studio is freaking out about it. I have tried to fix this using escape characters, but it still won't recognize the whole phrase.
Here is the phrase without the escape characters:
string exceptionPattern = #"(?:(?:"([^"}]*)")|(\w+))\s*:\s*(?:(?:"([^"}]*)")|(\w+))";
With just this code in, nothing else, almost every line in my code gets affected. Here is the code using escape characters:
string exceptionPattern = #"(?:(?:\"([^\"}]*)\")|(\w+))\s*:\s*(?:(?:\"([^\"}]*)\")|(\w+))";
Once this comes into play, only this line is not working. In VS, the ([^\ part close to the beginning is not highlighted, meaning that it is not in quotes. Does anyone have any idea on how I can fix this problem?
This is the string I am trying to match. Note: THIS IS NOT JSON! I have confirmed it many times with the developer who made the database where this sample is coming from and he confirmed it is not JSON, so please do not try to use JSON on this. Also, the regex I have is trying to match displayException and the message after it, and also exception and the message after that including success false, using the quotes as a point of splitting
{"data":"","displayException":"Invalid Account Status. Please complete the registration process by clicking the verification link in your eTTek Dash Registration Verification email. Please contact 1-800-341-6184 M-F 9a-5pm CT for further assistance.","exception":"UNABLE TO LOGIN","success":false}
Inside a verbatim string, to escape a double quotes you must need to add another double double quotes near to that like "". So the compiler treats "" as a double quotes or otherwise it would treat " as an end of the verbatim string.
Related
I'm working with matching an entire string within single quotes. The problem is, these strings are dynamically generated and I need to ignore all other single quotes within the first set of quotes. I've come across other solutions that are similar but I can't seem to tweak them to my needs.
Here is what I've worked with so far:
'(?:''|[^'])*'
I would like to match essentially everything within the first and last single quotes between content: and ;
Some example text:
#bottom {
content: 'Here we have an embedded unescaped 'single' that is generated at runtime. {Let's ignore it
please'
;
}
This is the playground I've been working in:
https://regex101.com/r/ITHciu/2
Any help would be greatly appreciated.
If you absolutely have to use Regexes for this and you are certain that ; will not be inside the string you are searching for, you could try this: '[^;]*'\s*;$. It will select everything from a ' and go until a like that ends with whitesapce and a ;.
Edit: if you need the stuff between the ' and ';, you could use a group '([^;]*)'\s*;$.
However, a much cleaner solution would be to make a little parser, that will read the string char by char. It's a fun exercise if you got a little bit more time.
If nothing else, you could use that regex to correct the invalid syntax in your files. And tell the people manually writing them what the valid syntax should be.
I am trying to see if a large string contains this line of HTML:
<label ng-class="choiceCaptionClass" class="ng-binding choice-caption">Was this information helpful?</label>
As you can see, this snippet has quotations in multiple places and it's causing problems when I do something like this:
Assert.IsTrue(responseContent.Contains("<label ng-class="choiceCaptionClass" class="ng - binding choice - caption">Was this information helpful?</label>"));
I've tried both of these ways of defining the string:
#"<label ng-class=""choiceCaptionClass"" class=""ng - binding choice - caption"">Was this information helpful?</label>"
and
"<label ng-class=\"choiceCaptionClass\" class=\"ng - binding choice - caption\">Was this information helpful?</label>"
But in each case the Contains() method looks for the literal string with either the double quotes or the backslashes. Is there another way I could define this string so I can correctly search for it?
Escaping the double-quotes with backslashes is the proper thing to do.
The reason your search may be failing is that the strings don't actually match. For example, in your version with backslashes, you have spaces around some of the dashes but your HTML string does not.
Try using regular expressions. I made this one for you but you can test your own regex here.
var regex = new Regex(#"<label\s+ng-class\s*=\s*""choiceCaptionClass""\s+class\s*=\s*""ng-binding choice-caption""\s*>\s*Was this information helpful\?\s*</label>", RegexOptions.IgnoreCase);
Assert.IsTrue(regex.IsMatch(responseContent));
If this is not working use the tester tool to figure it out what part of the pattern is getting off.
Hope this help!
This might seem like a question that's been answered many times. My team and I have tried many solutions over the past hour without any luck. We have a database driven string value that contains c:\test and we want to replace the backslash with \\ resulting in c:\\test.
We've tried using .Replace, Regex.Replace, .Split and rebuilding the string, I tried using a for loop and substring to examine each character. When you get past the colon the next character shows up as "\t".
Please try the solution before submitting as we've tried a lot of different methods including dozens of suggestions already on stack overflow.
If we manually set the string as a literal like path = #"c:\test" then using replace works fine.
I would think that the solution would be to create a string that doesn't process the escape character but I have no idea how to implement that.
Sounds like your string already contains "tab" character ('\t') you probably need to replace it with "\\t" :
var result = "c:\test".Replace("\t", "\\t");
I've tried for quite a long time to figure out whats going on but I've not found anything anywhere that someone besides me has ran into this issue.
I'm simply trying to hard code a path into a string. Easy stuff. Well for some reason
string fullPathSourceFile = #"c:\SQLSOURCE.txt";
is evaluating to c:\\SQLSOURCE.txt
I've tried everything to evaluated it to a single backslash remove the double quotes and it wont work. I even tried Replace(#"\\", #"\") and it has no affect. Anyone have any idea what's going on with my code that would force a double backslash when a single one should be evaluated? This is driving me nuts and it's so damn easy yet causing me a lot of frustration.
I'm then using the string variable below:
using (StreamReader reader = new StreamReader(fullPathSourceFile))
{
string line;
while ((line = reader.ReadLine()) != null)
{
sqlDBsource = line.ToString();
}
reader.Close();
}
Thanks to everyone for their input which helped my figure out what I was doing wrong. In Visual Studio (which is confusing) when you look at the value of a string in the debugger, it puts escapes in for you, so a double-backslash in a watch window or variable value popup is normal and does not mean there are actually two backslashes. When you mouse-over the variable or watch it in the watch window, click the magnifying glass icon at the right hand side of the tooltip/pane, this will show you the unescaped string at it would be printed to the console. Another way to display the actual results is: Console.WriteLine(the_problem_string); The issue I was having with the code is outside the scope of the post but the confusion of the results I was seeing from Visual Studio lead me to believe the string was the source of the problem when it wasn't.
This was a weird one. So I removed the verbatim as suggested in the comments and it worked when I used the double backslashes in the string. For some reason the code did not like the verbatim string and was translating the backslashes incorrectly. This resolved the issue. If anyone runs in to this you may need to play with the verbatim/non-verbatim strings because in some circumstances the compiler prefers non-verbatim.
Good Day.
I want to send an email from my code file in ASP.NET.
I want to apply styling to my html...How do I do it?
Here is the html part:
var Message = string.Format("<html><head></head><body><div style=""this does not work!!"">{0}<br/>{1}<br />{2}<br />{3}<br />{4}<br />{5}</body></html>", emaildetail.Name, emaildetail.Surname, emaildetail.CardProvider, emaildetail.CardType, emaildetail.CardNo, emaildetail.SuspendGuid);
SendEmail("my email address", "Suspended Message", Message, true, null, true, 587);
I tried using double quotes, but it does not work.
How can I do this?
Thank you
Either use a # prefix:
#"Some text ""here""";
or use a backslash to escape the quotes:
"Some text \"here\"";
Generally in such circumstances it's much less work and more pretty (as much as that can be) to use single quotes within the double quotes of a literal string; of course you can escape the doubles with a backslash, or a prefixed #, but there's very little point, and more typing or confusion:
<html><head></head><body><div style='this will work!!'
However, another crux of your problem might exist even so, as clients may render styles at discretion.
var Message = string.Format(#"<html><head></head><body><div style=""this does not work!!"">{0}<br/>{1}<br />{2}<br />{3}<br />{4}<br />{5}</body></html>", emaildetail.Name, emaildetail.Surname, emaildetail.CardProvider, emaildetail.CardType, emaildetail.CardNo, emaildetail.SuspendGuid);
You need to escape your string using the #.
See this post
Escape Double Quote
use \ as an escape character before your quotes...
var Message = string.Format("<html><head></head><body><div style=\"\">")