C# mongodb parsing "+" - Quantifier{x,y} following nothing - c#

I am getting the above error when trying to search a field in mongodb collection using C# driver.
Below is the line of code where i am getting this error:
IMongoQuery query = Query.Matches(field, BsonRegularExpression.Create(
new Regex(searchCri, RegexOptions.IgnoreCase)));
I tried replacing the searchCri variable with Regex.Replace(searchCri, "[~#%&*{}/<>?|\"-]+", "") however it did not fix my problem.
Could anyone assist?

This error occurs because the searchCri starts with + or is following a non-quantifiable subpattern.
You need to implement Regex.Escape and use something like:
new Regex(Regex.Escape(searchCri), RegexOptions.IgnoreCase)

Related

UiPath: How to set default values to a String[] variable

I have an exception when I'm trying to launch my workflow. It cames from my array declaration where I apparently don't have the right syntax.
My syntax really looks like the one used in the UiPath tuto I follow :
The tuto syntax:
My syntax:
I don't understand how I'm supposed to write the default values for a simple array of strings...
I'm only trying to pass the values in a Write Line activity, but this shows up when compiling:
If anyone can help, I'm stuck... Thanks
--- EDIT ---
The exception message is the following one:
Message: Compilation failure:
; expected Unvalid expression terms ',' ... many times...
The full results are stored in the Data property of this exception. Correct the errors of the source, then retry the load.
I found the problem: I did not instantiate the array with new String[] at the beginning. I suppose that the tutos are written using VB and not C#, which can explain the difference.
your declaration is correct maybe you have error in the loop here is a working example

Inserting variable inside regex

I am trying to use variable inside the below regex but I am unable to get the correct output.
(?<=="Page 2" class="fl" href=")(.*?)(?=\"><span class=\"csb)
--
I tried below but not sure I missed something.
(("?<== \"Page "+ pages++ + "\" class=\"fl\" href=\"")"(.*?)"(?=\"><span class=\"csb"));
--
How I can achieve it?
Edit:
aria-label="Page 2" class="fl" href="/search?q=.net+regex+tester&ei=p3Q7XNzkBt6RwgPrga-YBQ&start=10&sa=N&ved=0ahUKEwic1OvFo-vfAhXeiHAKHevAC1MQ8tMDCHw"><span class="csb ch"
--
Added the source.
We need to view on this issue not like "how to write correct regex query".
Just look like whole c# regex variable (object).
Your code in c# looks like (added double quotes)
var reg = new Regex(#"(?<==""Page 2"" class=""fl"" href="")(.*?)(?=\""><span class=\""csb)");
So, we need to String.Format the Regex string query, like
var reg = new Regex($#"(?<==""Page {page}"" class=""fl"" href="")(.*?)(?=\""><span class=\""csb)");
Btw, if we are talking about parsing html, it's strongly not recommended to use Regular Expression. More info

Razor.Parse error during parsing googlemap link

Scenario
In my app I parse text, links and tags to HTML using Razor.Parse<>().
string Razor.Parse<TEmail>(string razorTemplate, TEmail model) where TEmail : BaseEmail
I use '#' to parse data from model. I also parse web links. This feature works fine, till I parse Google Map links e.g.
Problem
https://www.google.com/maps/place/New+York,+NY,+USA/#40.6974034,-74.1197634,11z/data=!3m1!4b1!4m5!3m4!1s0x89c24fa5d33f083b:0xc80b8f06e177fe62!8m2!3d40.7127753!4d-74.0059728
Link includes '#'. So I get error:
RazorEngine.Templating.TemplateParsingException: '"40.6974034" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid.'
My try
I try to parse html with dobule ##
https://www.google.com/maps/place/New+York,+NY,+USA/##40.6974034,-74.1197634,11z/data=!3m1!4b1!4m5!3m4!1s0x89c24fa5d33f083b:0xc80b8f06e177fe62!8m2!3d40.7127753!4d-74.0059728
But i get error:
RazorEngine.Templating.TemplateParsingException: '"#" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid.'
Any idea what can I do? Thank you
This is because you are trying to pass something that is invalid. when you use the "#" keyword you cannot pass values, you must pass an identifier, keyword, comment, "(", "{". The "#" keyword marks the beginning of a code block for example:
string value = Razor.Parse("#UserSession.NumberOfPeople")
The above will work but this:
string value = Razor.Parse("#http://www.something.com")
won't work, this is invalid.
What you need to do is find an alternative.
you need to use razor engine. If you have no idea it is,
read here. You cannot esacpe #, because Razor.parse does not escape strings.

Regex.Unescape exception

The following folder path stored on a database table as \\SnowAngel\IcedData. However when reading from the database it is coming as:
string myFolderName = "\\\\SnowAngel\\IcedData"; Where SnowAngel is the server name.
Regex.Unescape(myFolderName);
The above line throws the following exception:
{"parsing \"\\SnowAngel\IcedData\" - Unrecognized escape sequence \I."}
What I'm missing here ?
One has to deal with two parsers, the first is the C# language and the second is the regex parser. You have added multiple slashes to speak to the C# parser and that is confusing to the regex parser.
I recommend that you use the C# literal # when dealing with regex patterns. That way one doesn't have to worry about the C# parser. Simply change it to
string myFolderName = #"\\SnowAngel\IcedData";
and work with it in regex, though that doesn't look like a pattern.

Tweaking my search functionality

I have tested search functionality i have implemented on a live website. I came across some small issues. I can't put special characters in the search box or my application will crash.
I tried to solve this using some replaces on the characters it crashes on, but this won't cure the pain.
When i entered this sign: * into the searchbox it gave me the following error:
Cannot parse '<%%> echo;': '' or '?' not allowed as first character in WildcardQuery. I have had this error before and then stripped the spaces between all words. The error was then gone. However when i now replace this: * with this: "" i will get the error described above.
Is there any standard way i can solve the special character issue with?
I'll write down some of my code here, so i can get better feedback.
Analyzer analyzer = new StandardAnalyzer();
QueryParser qpContent = new QueryParser(Index.ContentFieldName, analyzer);
keyword.Trim();
keyword = keyword.Replace("\"", "");
keyword = keyword.Replace("^", "");
keyword = keyword.Replace("*", "");
Query queryContent = qpContent.Parse(keyword + "*");
QueryParser qpLanguage = new QueryParser("language", analyzer);
Query queryLanguage = qpLanguage.Parse(Sitecore.Context.Language.Name.ToString());
As you see i first replace * and then later on add it back in the queryparser. I'm not 100% familiar with this kind of functionality and therefore have no clue at all what i'm doing wrong. All help is much appreciated, thanks!
you may have the ValidateRequest option set in your config, this helps to protect against Injection Attacks in asp.net.
Some details can be found here...
http://msdn.microsoft.com/en-us/library/bb355989.aspx
http://msdn.microsoft.com/en-us/library/system.web.configuration.pagessection.validaterequest.aspx
and...
http://en.wikipedia.org/wiki/Code_injection
http://en.wikipedia.org/wiki/SQL_injection

Categories