I am trying to retrieve a regex expression from database and pass it on to client (Browser). But the regex expression is getting slightly modified in between due to intermidiate parsing and regex expression processing in C#. Can anyone please tell me what I need to do inorder to pass the regex correctly to client. I have the following piece of code
private static readonly Regex defaultObjectPattern = new Regex(#"([^\w\.$\]])(\['?[$\w \s]*'?\][^\.\[])", RegexOptions.Compiled);
var parsedFormula = new StringBuilder(defaultObjectPattern.Replace(rule.Rule, "$1item$2"));
where rule.Rule is
"Error(\"![Item].[XYZ ID].match(/^20[^\s]{4,}$|^$/)\", \"Invaid XYZ
ID\",\"XYZ ID entered is Invalid. Please obtain a valid XYZ ID from
your Supervisor or delete this entry.\", \"XYZ ID\",\"All\")"
By the time above instruction is complete the regex expression in the match method is getting modified as /^20\['^\s']{4,}$|^$/. Single quotes are getting added within the Square brackets.
The exact string I am storing in the database is
'Error("![Item].[XYZ ID].match(/^20[^\s]{4,}$|^$/)", "Invaid XYZ
ID","XYZ ID entered is Invalid. Please obtain a valid XYZ ID from your
Supervisor or delete this entry.", "XYZ ID","All")'
I cannot change the defaultObjectPattern as it is used for lot of other things. But I need to get the regex expression in match method without getting modified (without single quotes getting added).
Thanks in advance for your help.
Related
I want to find User Principal names inside all sort of strings for a C# tool to anonymize customer specific data.
I found this one:
(?[^#]+)#(?(?:[a-z0-9]+.)+[a-z]+)
But in a string like this:
f-4af0-86e8-01439a0ae52a\",,\"Active\",\"10/30/2018 9:05:35
AM\",\"SingleSession\",\"Desktop\",,\"10/29/2018 2:35:06
PM\",,\"655952\",\"DOM\na010318\",\"na010318\",\"DOM\n010318\",\"S-1-5-21-2052699199-3915784498-1582209984-1157056\",\"user.a#domain.acc.local\",\"Primary\",\"c46b084c-6df3-47dd-9d3e-8e17f855c7fe\""
It matches the entire part before the UPN (the first space, because it matches the word I guess.
How can I re-write the regular expression to only find the e-mail/UPN within this string?
Thanks
If a UPN effectively has the same format as an email address - try some of the answers from this question:
regex extract email from strings
The following regex works for your example:
([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)
https://regex101.com/r/ijG8Xr/2
I want to set redirection from
www.somesite.com/products/dynamicstring/randomtext1/randomtext2
to www.somesite.com/products/dynamicstring
Is it possible to do that through Regex ?
It means if my incming url is
www.somesite.com/products/myproducts/test1/test2 it should redirect to www.somesite.com/products/myproducts/
just briefing more about this :
#TomLord i am using HttpContext.Current.Response.RedirectPermanent(matchingDefinition.To) i have all the redirects "From" and "To" in a class object, in the form of REGEX expressions.Example in From "/product/*" and To "/products" , i am reading these object and trying to redirect them, but i am not able to redirect something like /products/dynamicstring/randomtext1/ to /products/dynamicstring where dynamic string is random string , i dont find any regular expression which can be use to do this. For example /products/samples/randomtext1 should redirect to /products/samples/
Redirection cannot be done with regex alone. Google a bit what is a regular expression in reality. The short answer is: it's string-like expression that describes search pattern. So it can't redirect, not even replace a substring with substring or do anything else then match and capture parts of the matched string.
That being said, regex can help us do what you wanna. I am gonna assume you can use Javascript, cause I can't put a solution in every language. I am also gonna assume you will try to go over the code not copy paste and press enter. If you only need that hire a programmer. If you use another language, principle should be the same:
obtain URL
define regex
use capture group to extract the part of your URL that you need
construct a new URL
redirect to it
While matching the URLs in general is a fair bit more complex, like:
^(?:https?://)?(?:[\w]+\.)(?:\.?[\w]{2,})+$
As long as you are sure you will only be getting URLs and in the format you wanna, we will do it far simpler.
Basically, let's say you have:
some text with 2 dots that ends in com
then a /products/dynamicstring/
then text
then /
then text
As a regex that is:
/\w*.\w*.com\/products\/dynamicstring\/\w*\/\w*/g
Curde matching is done, but we still need to add a capture group we will use to extract part of the string we need:
/(\w*.\w*.com\/products\/)dynamicstring\/\w*\/\w*/g
Oke, now let's leverage this regex to do rest of the work:
Define regex:
var regex = /\w*.\w*.com\/products\/dynamicstring\/\w*\/\w*/g;
Get current URL. If you already have URL use it.
var currUrl = window.location.href;
Extract capture group from string:
var match = regex.exec(currUrl);
Use that to get a new URL from old one:
var redirectUrl = match[1] + myproducts/
Finally, we redirect with:
window.location.replace(redirectUrl);
I wrote all this straight from my head so I recommend you go over each step, look how it works, read some documentation about functions used. You might find an error as well as learn a lot.
Hi all I need to extract Guid from the following string
<PageFieldFieldValue:FieldValue FieldName='fa564e0f-0c70-4ab9-b863-0177e6ddd247' runat='server'></PageFieldFieldValue:FieldValue>
<PageFieldRichImageField:RichImageField FieldName="3de94b06-4120-41a5-b907-88773e493458" runat="server"></PageFieldRichImageField:RichImageField>
What i need is to get is "fa564e0f-0c70-4ab9-b863-0177e6ddd247" and "3de94b06-4120-41a5-b907-88773e493458" in this case, However this guid is dynamic and will change every time and there are lot more guids in the string that i have and I need to get all those guids so that I can add them to a colection.
Note: The string is actually an aspx page content. All nodes are different but have same property "FieldName" which I need to get.
I went through the link C# RegEx string extraction and construcked the regex in same way. Here is what I did :
string s = #"<PageFieldFieldValue:FieldValue FieldName='fa564e0f-0c70-4ab9-b863-0177e6ddd247' runat='server'>
</PageFieldFieldValue:FieldValue>";
Regex reg = new Regex(#"FieldName=(?<ReferenceId>{36})");
Match match = reg.Match(s);
string guid = match.Groups["ReferenceId"].Value;
How ever this didnt work for me. I get exception"parsing "FieldName=(?{35})" - Quantifier {x,y} following nothing." while creating the Regex object "reg".
If i dont use {36} which is suppose to be the length of GUiD:
Regex reg = new Regex(#"FieldName=(?<ReferenceId>)")
I dont get any exception but I dnt get desired result either. match.Groups["ReferenceId"].Value returns empty string
Try using sth. like that:
(?<=FieldName=['"])[a-f\d]{8}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{12}(?=['"])
Explanation
(?<=FieldName=['"]) prepended by FieldName= and " or '
[a-f\d]{8}-[a-f\d][...] followed by GUID (which is what is actually matching)
(?=['"]) followed by " or '
See this in action at Regex101
The issue you are having is basically that you are providing the quantifier {36} but are not telling it what to quantify - you need some character matching expression right before the quantifier. For, example I just added the '.' before {36} in your example (meaning "match any 36 characters") and it seems to work. Oh, and I also added the missing apostrophe after "FieldName=":
Regex reg = new Regex(#"FieldName='(?<ReferenceId>.{36})");
Working example: https://regex101.com/r/1tbien/1
I recently worked on a project to import Outlook Emails into OnBase Document Management System. Now I am in the process of enhancing this project.
When we receive an email, in the subject line, it contains numbers. I want to grab those numbers. So lets say if subject line contains:
"My name is Hiren and my Driver license# 123456".
I want to pull that sub-string 123456 to populate a "Driver License" keyword box, in OnBase. The length of the numbers is 6.
How can I do that?
This really has nothing to do with OnBase or any other integration. You simply need to know how to extract a number from a string. Where you store it is irrelevant. A simple way to do it would be using a regular expression:
var s = "My name is Hiren and my Driver license# 123456";
Regex r = new Regex(#"\d+");
foreach (var match in r.Matches(s))
Console.WriteLine(match);
You can do it in two ways: Either using RegEx or Looking at the last 6 characters of the subject line if you can guarantee the structure.
Here is an example:
http://dotnetpad.com/lB2pmbs7
If you are using mailBox importer, then is very easy, there is a keyword named "mail subject" that store the subjet of the email. Then with VBScript you can get the numbers.
This could help you:
Create an action to create an expression, then type this VBScript:
right(%K00122;len(%K00122) - InStr(%K00122;"#"))
Replace the "%K00122" wiht the number that represents the key word tha have the subject.
I'm trying to store Regex values in the DB, later to be used for custom validation.
I'm storing a Regex like this:
[a-zA-Z\''\"]+
(Two single qoutes in order to get one in the DB):
[a-zA-Z\'\"]+
When i extract this regex, I get an error while Filling the dataset:
Incorrect syntax near '\'. Unclosed quotation mark after the character string ']+''.
UPDATE #TEMP2 SET CandidateNameRegex='[a-zA-Z\'\"]+'
I've tried different variations:
'[a-zA-Z\''\"]+'
'[a-zA-Z\''\""]+'
'[a-zA-Z\''\\"]+'
'#[a-zA-Z\''\"]+'
'[a-zA-Z\'\"]+'
But none seem to do the trick.
So, How do we extract single quote from the DB without breaking the string?