How do extract a substring using regular expressions in Chapel? - regex

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!");

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("")

parse google document for text and copy result to sheet

I wish to parse a series of documents in a Google Drive folder using regular expressions.
The documents contain equipment model and serial numbers. I wish to then copy the results to a google sheet row by row. I have managed a similar task with emails successfully but to no avail with google docs.
Can anyone offer some guidance. I have tested the regular expressions in the 'find and replace' menu in google docs and they work fine. The following is simply an attempt to see if I can capture some data and write it to a cell in the active sheet.
function write() {
var ss= SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var doc =
DocumentApp.openById('1ZNqJjSJo1wkD3eaCRTY64g98hYEY77D4MDU6XpvA4MI');
var body = doc.getBody();
var text = body.findText('(\W|^)GSS\d{2}H(\W|$)')
ss.getRange(1,1).setValue(text);
}
You want to retrieve all values matched by (\W|^)GSS\d{2}H(\W|$) in the document, and put the result to spreadsheet with row by row. If my understanding is correct, how about this modification? I think that there are several answers for your situation. So please think of this as one of them.
Modification points :
Retrieve text from document.
Retrieve all matched values using the regex.
For this situation, I used RegExp#exec.
Put the result to spreadsheet.
Modified script :
function write() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var doc = DocumentApp.openById('1ZNqJjSJo1wkD3eaCRTY64g98hYEY77D4MDU6XpvA4MI');
var body = doc.getBody();
// Modified script
var text = doc.getBody().getText();
var result = [];
var r = /(\W|^)GSS\d{2}H(\W|$)/g;
while ((res = r.exec(text)) !== null) { // or while (res = r.exec(text)) {
result.push([res[0]]);
}
ss.getRange(ss.getLastRow() + 1, 1, result.length, 1).setValues(result);
}
If this was not what you want, I'm sorry. At that time, could you please provide the sample input and output you need? I would like to modify my answer.

How to validate data insertions and restrict them in Excel cells

I have an Asp.Net web application to manage certain tables in the database. I'm using Grid to insert, update the Database. In addition to this, the requirement is that, user should be able to insert into database from Excel(by uploading the Excel, sort of like Import from Excel into Database).
So, I'm reusing the code for insertions(which i used for Insert in Grid) for each row in the Excel.
And I have Regular expression validators for certain fieldsin Grid in Asp.Net as follows:
Id: can be combination of numbers,alphabets. Regex is:"^[a-zA-Z0-9_]{1,50}$"
Formula: can have arithmetic operators and dot. Regex is: "^[ A-Za-z0-9%._(/*+)-]*$"
Sort Order: must be nuber with some max size Regex is: "^[0-9]{1,5}$"
Weight: real number with max size Regex is : "^[0-9]+(?:\.\d{1,2})?$"
Domain UserName: username with domain name Regex is: "^[a-zA-Z\\._]{1,200}$"
I wanted to have this validators in the Excel cells too. I've searched if Excel allows Regular expressions and found that it should be done through vba or any third party tool. I don't know Vb.net and neither want to use any external tool.
And i don't know much about Excel too. Is there any way to do the validations. If so, will there be some formats for setting formula for regex.
Can anyone suggest me how to do this. Thanks In Advance.
You can use the Regex engine that comes with VBScript:
Dim User_ID As String
User_ID = InputBox("Enter User ID:")
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "^[\w]{1,50}$"
If .Test(User_ID) Then '// Check pattern matches User_ID string
Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Value = User_ID
Else
MsgBox("Invalid ID, please try again!")
End If
End With
I got the answer. I've wrote worksheet_Change event with if else
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Target.Row = 1 Then Exit Sub '// Only look at header row
Application.EnableEvents = False '// Disable events, prevent infinite loop.
If Cells(1, Target.Column).Value = "Attribute_Id" Then
Target.Value = AttributeId(Target.Value)
ElseIf Cells(1, Target.Column).Value = "Attribute_Name" Then
Target.Value = AttributeName(Target.Value)
End If
Application.EnableEvents = True '// Turn Events back on
End Sub
And these are the functions:
Function AttributeId(Attribute_Id As String) As String
With CreateObject("vbscript.regexp")
.Global = True
.Pattern = "^[a-zA-Z0-9_]{1,50}$"
.IgnoreCase = True
If Not .Test(Attribute_Id) Then
MsgBox ("Invalid Attribute ID, please try again!")
Exit Function
End If
End With
AttributeId = Attribute_Id
End Function
And
Function AttributeName(Attribute_Name As String) As String
If Attribute_Name = "" Then MsgBox ("Attribute Name is a Mandatory field!")
AttributeName = Attribute_Name
End Function
No need to bind the functions to the cells.
-- Thank you #S O for the help..

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);

Get Tridion Item ID with from URI 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