Get Tridion Item ID with from URI Regex - regex

If I have a Tridion URI like this 'tcm:1-23-8' and I want to get 23 with a Regular Expression.
The following works, but I know there is a better way. tcm: and '-8' are always there. The parts that change are 1 and 23.
var schemaUri = $display.getItem().getId(); // tcm:1-23-8
var re = /tcm:\d-/gi; // = 23-8
var schemaIdWithItemType = schemaUri.replace(re, "");
re = /-8/gi;
var schemaId = schemaIdWithItemType.replace(re, "");

If the number is always between the 2 dashes, you could do this:
var schemaId = schemaUri.split('-')[1];
This does the following:
split the string on the '-' character --> ['tcm:1', '23', '8'];
Get the second item from that array, '23'
Or, try this:
var schemaId = schemaUri.match(/-\d+-/)[0].replace(/-/g,'');
This'll find the number in between the dashes with .match(/-\d+-/), then remove the dashes.

Rather than calling $display.getItem().getId();, you can just call $display.getUri(); and then use the split()
var schemaId = $display.getUri().split('-')[1];

If you did want a pure Regex solution...
/^tcm:(\d+)-(\d+)(?:-(\d+))?$/i
Should validate your Tridion URI's format and provide you with 3 submatches, the second of which will be the Item ID

Related

Been trying to automate the Find and Replace in Google Sheets but did not work

My sheet is a query-sheet from database. Some of them contain html hex-code color which I need to manually use edit>Find and Replace every time it is refreshed.
I am very new to Google App Script and been trying to use the following code:
function Clearcode() {
var lookupone = new RegExp(/{color:#.{7}/);
var rep = "";
var spreadSheet = SpreadsheetApp.getActive();
var querySheet = spreadSheet.getSheetByName("QUERY");
var lastRow = querySheet.getLastRow();
var lastColumn = querySheet.getLastColumn();
var data = querySheet.getRange(2, 1, lastRow-1, lastColumn).getValues();
var textfinder = querySheet.createTextFinder(lookupone);
var found = textfinder.replaceAllWith(rep);
return (found);
}
Yet, when I run this function in the sheet it did not work. Any thought?
P.S. I planned to eliminated "[color]" part of the hex-code as well by create the similar function.
P.S.2 I have attached a snapshot of a table as you requested. The red line is just for confidentiality of the data. Below the line is just a normal text.
Pay attention to types!
CreateTextFinder accepts String as argument NOT a regexp object.
To use strings as regular expressions, useRegularExpressions needs to be set to true
querySheet.createTextFinder("\\{color:#?.{0,6}\\}")//only 6 characters
.useRegularExpressions(true)
.replaceAllWith("")

How do extract a substring using regular expressions in Chapel?

I know that Chapel has the Regexp Library but I don't understand how to use capturing groups. Could someone provide an example?
var template = "/home/user/:ID/details";
var uid = someKindaExtractyThing("/home/user/17/details");
writeln("So YOU are user ", uid, ", huh?")
> So YOU are user 17, huh?
This is my target.
The question already linked to the documentation so all that's really left to do is to show a code example.
use Regexp;
var input = "/home/user/17/details";
var capture:string;
var r = compile("""/home/user/(\w+)/details""");
var match = r.match(input, capture);
if match.matched then
writeln(capture);
else
writeln("not a match!");
The """ business will only work with master now or Chapel 1.17 or newer (otherwise you'd have to '\'-escape the '\' in a regular "string"). The Regexp module documentation has lots more details about what you can put in a regexp.
If you had multiple capture groups, you'd use more arguments to search to get them. search looks for a pattern within a string but match insists that the entire string match the pattern.
Here is an example with 2 capture groups:
use Regexp;
var input = "/home/user/17/details";
var part1:string;
var part2:string;
var r = compile("""/home/user/(\w+)/(\w+)""");
var match = r.match(input, part1, part2);
if match.matched then
writeln( (part1,part2) );
else
writeln("not a match!");

Removing a String from a String REGEX

I have an automatically generated string which looks as follows:
["January","February",null,"April"]
I need to remove any match of ",null" from the string, ie:
["January","February",null,"April"] --> ["January","February","April"]
How can I find everything except for ",null"?
I have tried variations of "^(?!,null).*" without success.
To answer your question as stated, you don't need regex:
str = str.replace(",null", "");
However, to handle the edge cases too:
["January","February",null,"April"] --> ["January","February","April"]
["January",null,null,"April"] --> ["January","April"]
[null,"January","February","April"] --> ["January","February","April"]
["January","February","April",null] --> ["January","February","April"]
[null] --> []
you would be better served with regex:
str = str.replaceAll("(?<=\\[)null,?|,null", "");
The replacement regex caters for null the first (and potentially only) position, and any other case.
Please review this one, we can use filter function
var arr = [null, "January","February",null,"April", null];
var arr2 = arr.filter(function(x){return x !== null})
console.log(arr,arr2);

how to remove string after specific character in list

I have a list as given below.
List<string> testing = new List<string>();
testing.Add("1(1)(r)");
testing.Add("2(4)");
testing.Add("3(w)");
testing.Add("33(4)");
testing.Add("5(4)");
testing.Add("6(6)");
Now my problem is that i want to remove full string on or after "(" my list should be something like as given below.
1
2
3
33
5
6
How can i solve this problem with lambda expression or is ther any other way to remove string after some specific character.
Thanks in advance
var result = testing.Select(x => x.Split('(')[0]);
Another way:
var result = testing.Select(x => x.Substring(0, x.IndexOf('(')));
Second approach will throw exception if one of your strings doesn't contain any (

As3 Regex or alternative to split strings

i have a html page , i use regex to remove all html tags from the page and extract the text using the below code.
var foo = loader.data.replace(/<.*?>/g, "");
var bar:Array = foo.split("Total");
foo = foo.split(bar[0]);
trace(foo);
And using the same code lines below the replace method i remove every string before the word "TOTAL". It does the job perfectly but now i want to apply and other split to get contents after "TOTAL" and remove the Content after "BYTES".
So when i try to split it up again with
var bar2:Array = foo.split("BYTES");
foo = foo.split(bar2[0]);
Flash returns a error saying SPLIT is a not a valid method :S
I tried several other ways , ( REPLACE ) but still flash produces errors.
Can Anyone help me to get through this ?
Thank you
".split()" is a method of String. When you did the assignment below:
foo = foo.split(bar[0]);
foo became an array, and thus the call
var bar2:Array = foo.split("BYTES");
was being made to an array, which is invalid (no such method)
What you want instead is this:
var foo = loader.data.replace(/<.*?>/g, "");
trace(foo);
var result = foo.split("Total")[1].split("BYTES")[0];
trace(result);