I have a string and I have to filter the following:
"#Subject = \"#hb\" + #uv_EmployeeID + \" fdsaas\" + #test"
I have to filter only #uv_EmployeeID and #test and not the values inside ""-inner double quotes
This is working : new Regex(#"[^""]#{1}[a-zA-Z_]+");
You just have to remove the first character from the result, like this :
var reg = new Regex(#"[^""]#{1}[a-zA-Z_]+");
var matches = reg.Matches("#Subject = \"#hb\" + #uv_EmployeeID + \" fdsaas\" + #test");
var empId = matches[0].Value.Substring(1); // #uv_EmployeeID
var test = matches[1].Value.Substring(1); // #test
Related
I have a two strings like this
mystr = "xyz/10021abc/f123"
mystr2 = "abc/10021abd/c222"
I want to extract 10021abc and 10021abd. I came up with
r = regexp.MustCompile(`(?:xyz\/|abc\/)(.+)\/`)
But when I want to extract the match using this:
fmt.Println(r.FindString(mystr))
It returns the entire string. How should I change my regex?
You can use FindStringSubmatch.
var re = regexp.MustCompile(`(?:xyz\/|abc\/)(.+)\/`)
var s1 = "xyz/10021abc/f123"
var s2 = "abc/10021abd/c222"
fmt.Println(re.FindStringSubmatch(s1)[1])
fmt.Println(re.FindStringSubmatch(s2)[1])
https://go.dev/play/p/C93DbfzVv3a
You could use a regex replacement here:
var mystr = "xyz/10021abc/f123"
var re = regexp.MustCompile(`^.*?/|/.*$`)
var output = re.ReplaceAllString(mystr, "")
fmt.Println(output) // 10021abc
I have a script in Google sheets
I am trying to find and replace headers on a sheet from a table of values on a different sheet
It is mostly working as desired but the replace is not working for any string that ends in ?
I do not know in advance when a ? will be present
I am using this:
const regex = new RegExp("(?<![^|])(?:" + search_for.join("|") + ")(?![^|])", "g");
I have tried to figure out how to correct my Regex but not getting it
Thanks in advance for your assistance with this
I have in a sheet:
search_for
replace_with
ABC Joe
MNQ
XYZ car
NNN XXX
DDD foo?
Bob bar
I have for Headers on a different sheet:
Label
Id
ABC Joe
XYZ car
DDD foo?
after running the replacement I want for headers:
Label
Id
MNQ
NNN XXX
Bob bar
what I get is:
Label
Id
MNQ
NNN XXX
DDD foo?
var data = range.getValues();
search_for.forEach(function(item, i) {
pair[item] = replace_with[i];
});
const regex = new RegExp("(?<![^|])(?:" + search_for.join("|") + ")(?![^|])", "g");
//Update Header row
//replace(/^\s+|\s+$|\s+(?=\s)/g, "") - Remove all multiple white-spaces and replaces with a single WS & trim
for(var m = 0; m<= data[0].length - 1; m++){
data[0][m] = data[0][m].replace(/^\s+|\s+$|\s+(?=\s)/g, "").replace(regex,(m) => pair[m])
}
A word of warning: what you're doing is scaring me a bit. I hope you know this is a brittle approach and it can go wrong.
You're not quoting the dynamic parts of the regex. The ? is a special character in regular expressions. I've written a solution to your problem below. Don't rely on my solution in production.
//var data = range.getValues();
var data = [
['Label', 'Id', 'ABC Joe', 'XYZ car', 'DDD foo?']
];
var search_for = [
'ABC Joe',
'XYZ car',
'DDD foo?'
];
var replace_with = [
'MNQ',
'NNN XXX',
'Bob bar'
];
var pair = {};
search_for.forEach(function(item, i) {
pair[item] = replace_with[i];
});
const regex = new RegExp("(?<![^|])(?:" + search_for.map((it) => quote(it)).join("|") + ")(?![^|])", "g");
for (var m = 0; m <= data[0].length - 1; m++) {
data[0][m] = data[0][m]
.replace(/^\s+|\s+$|\s+(?=\s)/g, "")
.replace(regex, (m) => pair[m]);
}
// see https://stackoverflow.com/a/3614500/11451
function quote(s) {
var regexpSpecialChars = /([\[\]\^\$\|\(\)\\\+\*\?\{\}\=\!])/gi;
return s.replace(regexpSpecialChars, '\\$1');
}
Can you not do something really simple like escaping all non-alphanumeric characters which would work with the example data you gave above and this seems trustworthy
function quote(s) {
var regexpSpecialChars = /((?=\W))/gi;
return s.replace(regexpSpecialChars, '\\');
}
I have this kind of Url :
/clients/18378/offers/2219/items/32779
I'm trying to get an array with in it : 18378, 2219, 32779
I've try this code but unsuccessful :
let currentUrl = this.router.url; // = '/clients/18378/offers/2219/items/32779'
var regexRouteOffer = /\/clients\/(.*?)\/offers\/(.*?)\/items\/(.*?)/gm;
var match = currentUrl.match(regexArticleInOffer);
console.log("Test 1 >>", match); // => ["/clients/18378/offers/2219/items/"]
I've try with exec function but it give me only one of the number (first one only)
var matches = [];
for (var m = null; m = regexRouteOffer.exec(currentUrl); matches.push(m[1]));
console.log("Test 2 >> ", matches); //["18378"]
What I'm doing wrong?
You don't need the flags g (because you only want to match once), and m (because there's no need to turn on multiline mode). And finally the last .* is ungreedy, so it tries to match as few elements as possible (zero in this case), so remove all or at least the final ?.
let currentUrl = '/clients/18378/offers/2219/items/32779'
var regexRouteOffer = /\/clients\/(.*)\/offers\/(.*)\/items\/(.*)/;
var match = currentUrl.match(regexRouteOffer);
console.log(match[1]); // 18378
console.log(match[2]); // 2219
console.log(match[3]); // 32779
I did a script (container-bound script) in my Spreadsheet in which I have 4 columns: (1) text before, (2) text after, (3) text to insert between, and (4) a URL of a Google Doc with the text in which I want to replace with the right value (between).
My method replace is not working when I have a french text (with characters like ç , à, è ) but with an english text it works fine How to solve this? Thank you very much for your help any idea is welcomed this is what I've done so far https://drive.google.com/drive/folders/1dOVNMrzEHvi3-vU3nbftK3Xoinxscrkn and my code :
/** It works for a text without accents :) but not for a french text :( **/
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Update the Google Doc") ;
var lastColumn = sheet.getLastColumn();
var numRows = sheet.getLastRow();
var COLUMN_URL = 3 ;
var data = sheet.getRange(1,1,numRows,lastColumn).getValues();
var start = 1;
var URL = data[start][COLUMN_URL];
Logger.log(' URL ' + URL);
var body = DocumentApp.openByUrl(URL).getBody();
var text_before = sheet.getRange(start + 1,1).getDisplayValue().replace(/[”|-’]/g,".");
Logger.log("text_before is " + text_before );
var text_after = sheet.getRange(start + 1,2).getDisplayValue().replace(/[”|-’]/g,".");
Logger.log("text_after is " + text_after );
var text_between = sheet.getRange(start + 1,3).getDisplayValue().replace(/[”|-’]/g,".");
Logger.log("text_between is " + text_between );
/** replace in the body of the Google Doc **/
// important to do this for the apostrophe and the " symbols that are different put the symbol in the cell
body.replaceText("\\Q’\\E","'");
// works
body.replaceText("\\Q”\\E",'"')
// ???? replace all unsupported characters from sheet means in my cell
/** symbols to test which works >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ok for the McDonald*?()\.,;%#(){!s . how about the " ***/
body.replaceText( "\\Q" + text_before + "\\E" + ".*?" + "\\Q" + text_after + "\\E", text_before + text_between + text_after );
/** another example **/
var start_bis = 2;
var text_before_bis = sheet.getRange(start_bis + 1,1).getDisplayValue().replace(/[”|-’]/g,".");
Logger.log("text_before is " + text_before_bis );
var text_after_bis = sheet.getRange(start_bis + 1,2).getDisplayValue().replace(/[”|-’]/g,".");
Logger.log("text_after is " + text_after_bis );
var text_between_bis = sheet.getRange(start_bis + 1,3).getDisplayValue().replace(/[”|-’]/g,".");
Logger.log("text_between is " + text_between_bis );
/** replace in the body of the Google Doc **/
body.replaceText( "\\Q" + text_before_bis + "\\E" + ".*?" + "\\Q" + text_after_bis + "\\E", text_before_bis + text_between_bis + text_after_bis );
}
You don't need to use replace. Just \\Q...\\E will work fine in this case. After replacing, . is considered a literal text due to (QE). Hence, it wasn't working.
Try
/** It works for a text without accents :) but not for a french text :( **/
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Update the Google Doc") ;
var lastColumn = sheet.getLastColumn();
var numRows = sheet.getLastRow();
var COLUMN_URL = 3 ;
var data = sheet.getRange(1,1,numRows,lastColumn).getValues();
var start = 1;
var URL = data[start][COLUMN_URL];
Logger.log(' URL ' + URL);
var body = DocumentApp.openByUrl(URL).getBody();
var text_before = sheet.getRange(start + 1,1).getDisplayValue();
Logger.log("text_before is " + text_before );
var text_after = sheet.getRange(start + 1,2).getDisplayValue();
Logger.log("text_after is " + text_after );
var text_between = sheet.getRange(start + 1,3).getDisplayValue();
Logger.log("text_between is " + text_between );
/** replace in the body of the Google Doc **/
// important to do this for the apostrophe and the " symbols that are different put the symbol in the cell
body.replaceText("\\Q’\\E","'");
// works
body.replaceText("\\Q”\\E",'"')
// ???? replace all unsupported characters from sheet means in my cell
/** symbols to test which works >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ok for the McDonald*?()\.,;%#(){!s . how about the " ***/
body.replaceText( "\\Q" + text_before + "\\E" + ".*?" + "\\Q" + text_after + "\\E", text_before + text_between + text_after );
As a newbie, I have tried a lot to solve the below problem.
My Current table
TestID TestName Name Url
1592461 Google-page (www.google.com)
1592467 Yahoo - Page (www.yahoo.com)
I am trying to split the data present in the column "TestName" and add the result to the columns "Name" and "URL" as given in the below table
Expected table
TestID TestName Name Url
1592461 Google-page (www.google.com) Google-page www.google.com
1592467 Yahoo - Page (www.yahoo.com) Yahoo - Page www.yahoo.com
I have tried to compile the following script but was unsuccessful.
function getUrl(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s1 = ss.getSheetByName("Sheet1");
var s2 = ss.getSheetByName("Sheet2");
var data = s1.getSheetValues(1, 2, s1.getLastRow() , 1);
var regExp = new RegExp("\(([^]]+)\)");
var row = [];
for(i = 0; i<data; i++) {
var url = regExp.exec(data)[i];
var output = s2.getRange("C2").setValue(url);
logger.log(url);
return url;
}
}
Could someone please help me in solving this.
In addition, I just wanted to let you know this can also be done with a (rather simple) formula. Enter in C1
=ArrayFormula(split(substitute(B2:B3, ")",""), "("))
Change range to suit.
I have an impression you want to get the data from Column 2 of the current spreadsheet into Column 3 and 4 in the same spreadsheet.
I suggest using the following regex:
var regExp = /(.*?)\(([^)]+)\)/;
The (.*?) will capture any 0+ chars other than line break chars into Group 1 (all before () then \( will match a ( and then ([^)]+) will capture 1+ chars other than ) into Group 2 (the URL) and then the \) will match a ).
And use it to analyze Column B data:
function getUrl(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s1 = ss.getSheetByName("Sheet1");
var src_range = s1.getRange("B:B"); // Grab the Column B range
var regExp = /(.*?)\(([^)]+)\)/; // Define the regex
for(i = 1; i<=src_range.getLastRow(); i++) { // Loop through all the cells in the range
if (!src_range.getCell(i, 1).isBlank()) { // If the cell is not blank, process it
var m = regExp.exec(src_range.getCell(i, 1).getValue()); // Run the regex
if (m) { // If there is a match
var text = m[1]; // Text to be placed into Column C
s1.getRange('C' + i).setValue(text);
var url = m[2]; // URL to be placed into Column D
s1.getRange('D' + i).setValue(url);
}
}
}
}
See a sample document.