i want that my input text repect this format :
start with P0E and 10 digits:
P0E0532313118
how i can do it with jquery or java script please ?
thanks,
Pure JS:
let value = 'P0E0532313118';
let isValid = /^(P0E[0-9]{10})$/.test(value); // = TRUE
Related
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!");
At the moment I'm converting my project to Swift 3.
I have a code block like this:
let someString = "asd.asABCDEFG.HI"
let regexp = "^\\w*[.]\\w{2}"
let range = someString.rangeOfString(regexp, options: .RegularExpressionSearch)
let result = someString.substringWithRange(range!)
The rangeOfString method is gone in Swift 3. Can somebody post an example, how a regexp search can be done. Thanks in advance.
In Swift 3 rangeOfString is like range(of:options:) and substringWithRange is like substring(with:).
if let range = someString.range(of:regexp, options: .regularExpression) {
let result = someString.substring(with:range)
}
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..
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
I'm trying to remove beggining numbers from a column in a Google Docs spreadsheet using regex. I can't get RegExReplace function to work. This is the error I get when I run/debug the code:
Missing ) after argument list. (line 14)
This is a part of my code (line 14 is the RegExReplace function line, bolded):
regexFormat = "^[0-9]+$";
replVal = value.RegExReplace(value; regexFormat; ""); //error here
rplc.setValue(replVal);
This is the official syntax: RegExReplace( text ; regular_expression ; replacement )
Anyone knows how to use this function? Thanks!
I don't know why the docs list a semicolon, but if you are doing it as a spreadsheet function, you still need to use commas. Try the following:
=REGEXREPLACE("What-A Crazy str3ng", "\W", "")
Which as expected, yields
WhatACrazystr3ng
I've found another solution for replacing with regexp in Google Docs Script:
var replace = '';//because we want to remove matching text
var regexp2 = new RegExp("[0-9]*[\.]*");//an example of regexp to do the job
var valcurat = value.replace(regexp2, replace);//working
As I did not find any solution for RegExReplace, I changed the method with replace(regexp, new_text). This one works.
This is just a guess but if the function is Javaish, maybe there are 2 forms.
Form 1:
myvar = RegExReplace(value; regexFormat; "");
Form2:
myvar = value.RegExReplace(regexFormat; "");