Append value of an XML Element with additional text using regular expressions - regex

I am trying to convert the following in an XML
<SharedDataSetReference>AnyDataSetName</SharedDataSetReference>
to
<SharedDataSetReference>/DataSets/AnyDataSetName</SharedDataSetReference>
at the same time, existing instances of
<SharedDataSetReference>/DataSets/AnyDataSetName</SharedDataSetReference>
should remain unchanged.
Pls note that AnyDataSetName can have
<SharedDataSetReference>Company</SharedDataSetReference>
<SharedDataSetReference>Customer</SharedDataSetReference>
or
/Datasets/Order
and they should change to
<SharedDataSetReference>/Datasets/Company</SharedDataSetReference>
<SharedDataSetReference>/Datasets/Customer</SharedDataSetReference>
<SharedDataSetReference>/Datasets/Order</SharedDataSetReference>

Try this
<SharedDataSetReference>([^\/]+)<\/SharedDataSetReference>
https://regex101.com/r/yM7tA0/1

Thanks Tim007
This is what i did
var sharedDatasets = Regex.Replace(datasets.First().ToString(), String.Format(#"<{0}>(?<DataSetReferenceValue>[^\/]+)<\/{0}>", "SharedDataSetReference"), delegate(Match match)
{
var value = String.Concat("/Datasets/", match.Groups["DataSetReferenceValue"].ToString());
return String.Format(#"<{0}>{1}</{0}>", "SharedDataSetReference", value);
}, RegexOptions.Multiline
)

Related

How to find matches that occur within a specified string with regex?

I have a unique situation where I need to query a mongo database to find the names of people who occur in a body of text. The query must specify the body of text and find records with values that occur in the body of text. How can I do this with a regular expression?
I need to write a query where this would match:
/Jonathan is a handsome guy/.test('Jonathan')
The problem is that the text inside "test" is the value of a mongo field, so this query must be written such that the body of text is provided as input, and it matches on names that occur within (are substrings of) the body of text.
A more concrete example:
db.test.find();
{ "_id" : ObjectId("547e9b79f2b519cd1657b21e"), "name" : "Jonathan" }
{ "_id" : ObjectId("547e9b88f2b519cd1657b21f"), "name" : "Sandy" }
db.test.find({name: { $in: [/Jonathan has the best queries/]} } );
I need to construct a query that would return "Jonathan" when provided the input "Jonathan has the best queries"
This $where may do the trick, though can be very slow:
db.test.find({$where: function() {
var mystr = '/Jonathan has the best queries/';
var patt = new RegExp(this.name);
if (patt.test(mystr)) return true;
return false;
}})

How to efficiently convert DataSet.Tables to List<DataTable>?

I see many posts about converting the table(s) in a DataSet to a list of DataRows or other row data but I was unable to find anything about this question. This is what I came up with using .Net 3.0:
public static List<DataTable> DataSetToList(DataSet ds)
{
List<DataTable> result = new List<DataTable>();
foreach (DataTable dtbl in ds.Tables)
{
result.Add(dtbl);
}
return result;
}
Is there a better way, excluding an extension method?
Thanks
Based on Why LINQ casting with a Data.DataTableCollection this will work;
List<DataTable> result = new List<DataTable>(ds.Tables.Cast<DataTable>())
IEnumerable<DataTable> sequence = dt.AsEnumerable();
or
List<DataTable> list = dt.AsEnumerable().ToList();

Replace expression with subsection using regex?

My IDE PHPstorm allows you to do search and replace using regex, one of the things I find myself often doing is switching the order or action, aka, in function a I will set a value on items from list a using list b as the values.
but then in function b I want to invert it.
so I want to set a value on items from list b using list a as the values.
A proper example is this:
var $clipDetailsGame = $('#clipDetailsGame');
var $clipDetailsTitle = $('#clipDetailsTitle');
var $clipDetailsByline = $('#clipDetailsByline');
var $clipDetailsTeamOne = $('#clipDetailsTeamOne');
var $clipDetailsTeamTwo = $('#clipDetailsTeamTwo');
var $clipDetailsReferee = $('#clipDetailsReferee');
var $clipDetailsDescription = $('#clipDetailsDescription');
var $clipDetailsCompetition = $('#clipDetailsCompetition');
function a(clip){
clip.data('gameId' , $clipDetailsGame.val());
clip.data('title' , $clipDetailsTitle.val());
clip.data('byline' , $clipDetailsByline.val());
clip.data('team1' , $clipDetailsTeamOne.val());
clip.data('team2' , $clipDetailsTeamTwo.val());
clip.data('refereeId' , $clipDetailsReferee.val());
clip.data('description' , $clipDetailsDescription.val());
clip.data('competitionId', $clipDetailsCompetition.val());
}
function b (clip){
$clipDetailsGame .val(clip.data('gameId'));
$clipDetailsTitle .val(clip.data('title'));
$clipDetailsByline .val(clip.data('byline'));
$clipDetailsTeamOne .val(clip.data('team1'));
$clipDetailsTeamTwo .val(clip.data('team2'));
$clipDetailsReferee .val(clip.data('refereeId'));
$clipDetailsDescription.val(clip.data('description'));
$clipDetailsCompetition.val(clip.data('competitionId'));
}
Excluding the formatting (It's just there to make my point clearer), what kind of regex could I use to do the replacement for me?
Basic regex -- nothing fancy or complex at all
Search for: (clip\.data\('[a-zA-Z0-9]+')\s*, (\$[a-zA-Z0-9]+\.val\()(\)\);)
Replace with: \$2\$1\$3
The only PhpStorm-related thing here is replacement string format -- you have to "escape" $ to have it work (i.e. it has to be \$2 to use 2nd back-trace instead of just $2 or \2 (as used in other engines)).
This will transform this:
clip.data('gameId' , $clipDetailsGame.val());
clip.data('title' , $clipDetailsTitle.val());
clip.data('byline' , $clipDetailsByline.val());
clip.data('team1' , $clipDetailsTeamOne.val());
clip.data('team2' , $clipDetailsTeamTwo.val());
clip.data('refereeId' , $clipDetailsReferee.val());
clip.data('description' , $clipDetailsDescription.val());
clip.data('competitionId', $clipDetailsCompetition.val());
into this:
$clipDetailsGame.val(clip.data('gameId'));
$clipDetailsTitle.val(clip.data('title'));
$clipDetailsByline.val(clip.data('byline'));
$clipDetailsTeamOne.val(clip.data('team1'));
$clipDetailsTeamTwo.val(clip.data('team2'));
$clipDetailsReferee.val(clip.data('refereeId'));
$clipDetailsDescription.val(clip.data('description'));
$clipDetailsCompetition.val(clip.data('competitionId'));
Useful link: http://www.jetbrains.com/phpstorm/webhelp/regular-expression-syntax-reference.html
Mopping up (not quite the answer to this question, but another way of organizing the code to make search and replace unnecessary):
var $details = {};
var fields = [
'Game', 'Title', 'Byline', 'TeamOne', 'TeamTwo', 'Referee', 'Description',
'Competition'
];
for(field in fields) {
$details[field] = $('#clipDetails' + field);
}
function a(clip) {
for(field in fields) {
clip.data(field, $details[fields].val());
}
}
function b(clip) {
for(field in fields) {
$details[field].val(clip.data(field));
}
}
Yes, I know that there are tiny naming inconsistencies that means that this isn't working out of the box, such as Game versus gameId. This is an excellent occasion to clean that up too :). If you still want to keep the title case for the ids (such as #clipDetailsGame in stead of #clipDetailsgame), keep it in title case in the fields array and use toLowerCase where you need lower case.
By the way, there is an interesting read on what makes DRY a good thing here: https://softwareengineering.stackexchange.com/questions/103233/why-is-dry-important

Linq query not matching hrefs

I'm trying to write out all matches found using a regex with the code below:
var source = "<Content><link><a xlink:href=\"tcm:363-48948\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">Read more</a></link><links xlink:href=\"tcm:362-65596\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"/></Content>";
var tridionHref = new Regex("tcm:([^\"]*)");
var elem = XElement.Parse(source);
XNamespace xlink = "http://www.w3.org/1999/xlink";
if (tridionHref.IsMatch(elem.ToString()))
{
foreach (var Id in elem.Elements().Where(x => x.Attribute(xlink + "href") != null))
{
Console.WriteLine(Id.Attribute(xlink + "href").Value); //For testing
Id.Attribute(xlink + "href").Value = Id.Attribute(xlink + "href").Value.Replace("value1", "value2"); //Just to show you an example
}
}
My console window outputs tcm:362-65596 but not tcm:363-48948. It looks like the code doesn't see the value of xlink:href inside my <a> tag as an attribute? Can anyone point me in the right direction? I need to match ALL instances of tcm:([^\"]*).
The problem is you are not looking in the right place. Your elem.Elements is looking at the link element and the links element. Only one of these has the attribute that you are looking for. You'll need to select the elements you want to check more precisely before looking for the right attribute.
I've got it working. I didn't need a regex I just needed to get the Descendants instead inside my for loop. foreach (var Id in elem.Descendants().Where(x => x.Attribute(xlink + "href") != null))

ASP.NET MVC Custom route regex to catch a substring of items and check for their existence

I'm trying to create a custom route for URL with the following format:
http://domain/nodes/{item_1}/{item_2}/{item3_}/..../{item_[n]}
Basically, there could be a random amount of item_[n], for example
http://domain/nodes/1/3/2
http://domain/nodes/1
http://domain/nodes/1/25/11/45
With my custom route I would like to retrieve an array of items and do some logic (validate and add some specific information to request context) with them.
For example from [http://domain/nodes/1/25/11/45] I would like to get an array of [1, 25, 11, 45] and process it.
So, I have 2 problems here.
The first one is a question actually. Am I looking in the right direction? Or there could be an easier way to accomplish this (maybe without custom routes)?
The second problem is matching incoming url with a regex pattern. Could someone help me with it?
Thanks in advance :)
To solve your problem I think that a way could be to create a routing class and then handle the params accordinlgy.
public class CustomRouting : RouteBase
{
public override RouteData GetRouteData(HttpContextBase httpContext)
{
RouteData result = null;
var repository = new FakeRouteDB(); //Use you preferred DI injector
string requestUrl = httpContext.Request.AppRelativeCurrentExecutionFilePath;
string[] sections = requestUrl.Split('/');
/*
from here you work on the array you just created
you can check every single part
*/
if (sections.Count() == 2 && sections[1] == "")
return null; // ~/
if (sections.Count() > 2) //2 is just an example
{
result = new RouteData(this, new MvcRouteHandler());
result.Values.Add("controller", "Products");
result.Values.Add("action", "Edit");
result.Values.Add("itmes0", sections[1]);
if (sections.Count() >= 3)
result.Values.Add("item2", sections[2]);
//....
}
else
{
//I can prepare a default route
result = new RouteData(this, new MvcRouteHandler());
result.Values.Add("controller", "Home");
result.Values.Add("action", "Index");
}
return result;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
//I just work with outbound so it's ok here to do nothing
return null;
}
}
In the global.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new CustomRouting());
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}
This should give you an idea of what I think. Hope it helps
I can't help you with the first part of your question, but I can have a go at creating the regex.
In your example all the items are digits - is that the only option ? If not, please provide more info on possible characters.
For now the regex would be:
#"http://domain/nodes(?:/(\d+))*"
(?:) is a non capturing group, () is a capturing group.
If you match all occurences, then you'll end up with groups 1-n, where each group will contain the matched number (group number 0 will be the whole match).