regex in string is being stripped out when converting to BsonDocument - c#

I am creating quite a complex query for mongodb within .net using C#. To do this I am building the query as a string then parsing it to get a QueryDocument:
var Q = new QueryDocument(BsonDocument.Parse(QueryString))
My problem is that part of the query contains a regex:
{""Str.tagkw"":{$regex : "" \\b(rasberry|ice cream|sweeties)\\b ""}}
After parsing the $regex part has been removed when I look at the query Q (as above)
Any help would be welcome.

Your code appears to work for me:
string queryString = #"{""Str.tagkw"":{$regex : "" \\b(rasberry|ice cream|sweeties)\\b ""}}";
var Q = new QueryDocument(BsonDocument.Parse(queryString));
When you look at this in an IDE such as Visual Studio, it will be displayed as
{ "Str.tagkw" : / \b(rasberry|ice cream|sweeties)\b / }
That's the Javascript representation: In Javascript, you can create regular expressions using either
var regex = new RegExp("(foo|bar)");
or, as syntactic sugar
var regex = /(foo|bar)/;
The ToString method which will be used by the debugger seems to prefer the second representation, but that's just a matter of how it's displayed.

Related

Extract substring between startsequence and endsequence in C# using LINQ

I have an XML instance that contains processing instructions. I want a specific one (the schematron declaration):
<?xml-model href="../../a/b/c.sch" schematypens="http://purl.oclc.org/dsdl/schematron"?>
There may or may not be more than these very processing instructions present, so I can't rely on its position in the DOM; it is guaranteed, on the other hand, that there will be only one (or none) such Schematron file reference. Thus, I get it like so:
XProcessingInstruction p = d.Nodes().OfType<XProcessingInstruction>()
.Where(x => x.Target.Equals("xml-model") &&
x.Data.Contains("schematypens=\"http://purl.oclc.org/dsdl/schematron\""))
.FirstOrDefault();
In the example given, the content of p.Data is the string
href="../../a/b/c.sch" schematypens="http://purl.oclc.org/dsdl/schematron"
I need to extract the path specified via #href (i. e. in this example I would want the string ../../a/b/c.sch) without double quotes. In other words: I need the substring after href=" and before the next ". I'm trying to achieve my goal with LINQ:
var a = p.Data.Split(' ').Where(s => s.StartsWith("href=\""))
.Select(s => s.Substring("href=\"".Length))
.Select(s => s.TakeWhile(c => c != '"'));
I would have thought this gave me a IEnumerable<char> which I could then convert to a string in one of the ways described here, but that's not the case: According to LINQPad, I seem to be getting a IEnumerabale<IEnumerable<char>> which I can't manage to make into a string.
How could this be done correctly using LINQ? Maybe I'd better be using Regex within LINQ?
Edit: After typing this down, I came up with a working solution, but it seems very inelegant:
string a = new string
(
p.Data.Substring(p.Data.IndexOf("href=\"") + "href=\"".Length)
.TakeWhile(c => c != '"').ToArray()
);
What would be a better way?
Try this:
var input = #"<?xml-model href=""../../a/b/c.sch"" schematypens=""http://purl.oclc.org/dsdl/schematron""?>";
var match = Regex.Match(input, #"href=""(.*?)""");
var url = match.Groups[1].Value;
That gives me ../../a/b/c.sch in url.
Please don't use Regex for general XML parsing, but for this situation it's fine.

Is there a way to Ignore escape characters but still use string interpolation? C#

So, the string I need to implement is this, I am using .Net 4.5.2 in c# in visual studio 2019, I want the espected output to be exactly as below albeit with FIRSTNAME being replaced by a variable.
beneficiaryFirstName: \\\"FIRSTNAME\\\"
This is being used with a lot of similarly structured strings to join them together to form a large graphQL query. The problem I have is that VStudio keeps throwing up errors.
Edit : I would like to make clear, I need the \'s in the string result, and I need FIRSTNAME to be treated as a variable.
I have attempted to use this.
$#"beneficiaryFirstName: \\\"{{FIRSTNAME}}\\\""
But get told that it there's unexpected characters "" and "".
What is the best way around this?
You just need the right escaping
var FIRSTNAME = "Bob";
var pad = #"\\\";
var test1 = $"beneficiaryFirstName: \\\\\\\"{FIRSTNAME}\\\\\\\"";
var test2 = #$"beneficiaryFirstName: \\\""{FIRSTNAME}\\\""";
var test3 = $"beneficiaryFirstName: {pad}\"{FIRSTNAME}{pad}\"";
Console.WriteLine(test1);
Console.WriteLine(test2);
Console.WriteLine(test3);
Output
beneficiaryFirstName: \\\"Bob\\\"
beneficiaryFirstName: \\\"Bob\\\"
beneficiaryFirstName: \\\"Bob\\\"
Disclaimer, I am not sure if the quotes are correct in your example, they seem like they are in weird places, though that could be just how it is
you can do like this.
string FIRSTNAME = "YourName";
string temp = $"beneficiaryFirstName: {FIRSTNAME}";
Console.WriteLine(temp);
It will print
beneficiaryFirstName: YourName
You can try this string temp = $"beneficiaryFirstName: \\\\{FIRSTNAME}\\\\"; if you want outout
beneficiaryFirstName: \\YourName\\
As mentioned by the OP, you can use this to get the required output.
string temp = #$"beneficiaryFirstName: \\\{FIRSTNAME}\\\";

Regex matching dynamic words within an html string

I have an html string to work with as follows:
string html = new MvcHtmlString(item.html.ToString()).ToHtmlString();
There are two different types of text I need to match although very similar. I need the initial ^^ removed and the closing |^^ removed. Then if there are multiple clients I need the ^ separating clients changed to a comma(,).
^^Client One- This text is pretty meaningless for this task, but it will exist in the real document.|^^
^^Client One^Client Two^Client Three- This text is pretty meaningless for this task, but it will exist in the real document.|^^
I need to be able to match each client and make it bold.
Client One- This text is pretty meaningless for this task, but it will exist in the real document.
Client One, Client Two, Client Three- This text is pretty meaningless for this task, but it will exist in the real document.
A nice stack over flow user provided the following but I could not get it to work or find any matches when I tested it on an online regex tester.
const string pattern = #"\^\^(?<clients>[^-]+)(?<text>-.*)\|\^\^";
var result = Regex.Replace(html, pattern,
m =>
{
var clientlist = m.Groups["clients"].Value;
var newClients = string.Join(",", clientlist.Split('^').Select(s => string.Format("<strong>{0}</strong>", s)));
return newClients + m.Groups["text"];
});
I am very new to regex so any help is appreciated.
I'm new to C# so forgive me if I make rookie mistakes :)
const string pattern = #"\^\^([^-]+)(-[^|]+)\|\^\^";
var temp = Regex.Replace(html, pattern, "<strong>$1</strong>$2");
var result = Regex.Replace(temp, #"\^", "</strong>, <strong>");
I'm using $1 even though MSDN is vague about using that syntax to reference subgroups.
Edit: if it's possible that the text after - contains a ^ you can do this:
var result = Regex.Replace(temp, #"\^(?=.*-)", "</strong>, <strong>");

Using literal underscore character in a MySQL query from c#

From my ASP.NET application I want to issue this query to MySQL
SELECT * FROM responses WHERE field_id LIKE '3\_%'
In other words I am looking for records where the second character is the underscore literal character.
The code generated by the model designer looks like this:
public virtual RiskAnalysis.responsesDataTable GetResponseGroupForAnalysis(int raid, string fieldid) {
this.Adapter.SelectCommand = this.CommandCollection[2];
this.Adapter.SelectCommand.Parameters[0].Value = ((int)(raid));
if ((fieldid == null)) {
throw new global::System.ArgumentNullException("fieldid");
}
else {
this.Adapter.SelectCommand.Parameters[1].Value = ((string)(fieldid));
}
RiskAnalysis.responsesDataTable dataTable = new RiskAnalysis.responsesDataTable();
this.Adapter.Fill(dataTable);
return dataTable;
}
If I call this function like so:
string filter_string = #"3\_%";
ResponsesAdapter.GetResponseGroupForAnalysis(10, filter_string);
the MySQL log reports the query to look like this:
SELECT *
FROM responses
WHERE (ra_id = 10) AND (field_id LIKE '3\\_%')
In other words the I know I'm missing something blindingly obvious here, but how do I place MySQL's escape backslash in the query without C# (un)helpfully escaping it?
In the standard SQL, you can use the ESCAPE keyword to set an escaping character.
MySQL follows the standard SQL in this, as stated by the LIKE predicate documentation:
expr LIKE pat [ESCAPE 'escape_char']
Like so:
...
field_id LIKE '3\_%' ESCAPE '\'
This will make the wildcard _ be treated as a string literal.
Hope this what you were asking about.
OK, it seems (by experiment - happy to hear explanation or documentation link) that in MySQL (field_id LIKE '3\_%'); is equivalent to (field_id LIKE '3\\_%'); and both will find records where field_id starts with '3_'. Got myself well confused there - thanks Mahmoud and René for trying to understand what I was getting at!

MDX parser in C#

I need to parse an MDX with my .Net application. Initially, I used regular expression to do this but the expressions are getting complicated and a regex expert suggested that it will be better if I use parser.
Is there any parser specifically for MDX? I tried Ranet but for some unknown reason it does not install in my machine (does not show any error message).
I need to split the several parts of the MDX into strings. For example, the where clause in one string, from clause in another etc.
The best solution would be to find a parser, but it is always very hard to find a parser for your specific needs. So if you end up with writing a parser Ve Parser is a better tool comparing to regex, because it provides more parsing functionalities, you can generate better output and since you are calling .net methods it implicitly have intellisence for writing your parser.
The downside is that it still is not well-documented, so you may find it difficult for some special scenarios.
Project Link : http://veparser.codeplex.com
NuGet identifier : veparser
If you need to get text for different parts of an MDX here is a partial sample code:
using VeParser;
using System.Linq;
using System.Collections.Generic;
using System;
public class MDXParser : TokenParser
{
protected override Parser GetRootParser()
{
// read the following line as : fill 'select' property of 'current object(which is a statement)' with the 'new value of selectStatement' after facing a sequence of a select statement and then the symbol of ( and then a delemitied list of identierfiers filling the 'fileds' property of 'current selectStatement object' delemitied by ',' and finally expect the sequence to be finished with a symbol of ')'
var selectStatement = fill("select", create<selectStatment>( seq(expectKeyword_of("select"), expectSymbol_of("("), deleimitedList(expectSymbol_of(","), fill("fields",identifier) ), expectSymbol_of(")"))));
// read the following line as : fill the from property of 'current object(which is a statement)' with an expected identifier that is after a 'from' keyword
var fromStatement = seq(expectKeyword_of("from"), fill("from", identifier));
// the following statement is incomplete, as I just wanted to show a sample bit, If you are interested I can help you complete the parser until the full documentation become available.
var whereStatement = fill("where", create<whereStatement>(seq(expectKeyword_of("where"))));
var statement = create<statement>(seq(selectStatement, fromStatement, whereStatement));
return statement;
}
public statement Parse(string code)
{
var keywords = new[] { "select", "where", "from" };
var symbols = new[] { "(",")", ".", "[", "]" };
var tokenList = Lexer.Parser(code, keywords, symbols, ignoreWhireSpaces : true);
// Now we have our string input converted into a list of tokens which actually is a list of words but with some additional information about any word, for example a "select" is marked as keyword
var parseResult = base.Parse(tokenList.tokens);
if (parseResult == null)
throw new Exception("Invalid Code, at the moment Ve Parser does not support any error reporting feature.");
else
return (statement)parseResult;
}
}
public class statement
{
public selectStatment select;
public string where;
public identifier from;
}
public class selectStatment
{
public List<identifier> fields;
}
public class whereStatement
{
}
This code is not complete, I just wanted to demonstrate how to use Ve Parser to write your own parser for MDX. If you liked the library and wanted to use it, I would be happy to provide you with the all descriptions and techniques you need.
You could take a look at parser generators like http://grammatica.percederberg.net/
Though it is hard work to formulate grammar and keep it up to date.

Categories

Resources