I'm trying to validate date string and for that I have written following regex string
[0-3]*\d{1}(st|nd|rd|th)?[\s\-\/]?(Jan|January|Feb|February|Mar|March|Apr|April|May|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December|[0-1]?\d{1})[\s\-\/,]?(\d{4}|\d{2})
string input for my regex is "31st March, 2018"
I have already included ,(comma character) in my regex string (the [\s\-\/,] part) but above input fails to validate.
Can anyone point out what correction is needed in above regex string so that it can detect ,(comma) character in string?
You're missing the space between the comma and the year. You should add \s? after the block that matches the comma.
[0-3]?\d{1}(st|nd|rd|th)?[\s\-\/]?(Jan|January|Feb|February|Mar|March|Apr|April|May|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December|[0-1]?\d{1})[\s\-\/,]?\s?(\d{4}|\d{2})
Also, you need not scape characters inside [], or specify a quantifier for matching only one character, so you can just change your regex to:
[0-3]?\d(st|nd|rd|th)?[ -/]?(Jan|January|Feb|February|Mar|March|Apr|April|May|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December|[0-1]?\d)[ -/,]?\s?(\d{4}|\d{2})
demo
PS: I don't really know much of C#, so can't speak for what others recommend using Datetime, but sounds like it may be worth checking out.
Related
I am working on a regex to validate that a string input contains letters with in a length of 6 to 20 and inside the string exists square brackets with an integer.
Something like:
ABC[15]WHATEVER
I get that it would be better to check for the string length without the the use of regex, but still wandering for the brackets with the integer.
What I managed to do:
\[([0-9\-]+)]
which is working by testing through regex tester .net
Is this the appropriate solution though?
Any help is welcome
I'd go with
^(?=[\w\[\]]{6,20}$)[A-Z]+\[\d+\][A-Z]+$
see this working example on Regex101
Explanation
^ Begin of string
(?=[\w\[\]]{6,20}$) Followed by a string between 6 and 20 characters long ({6,20}) containing only alphanumeric characters (\w) and brackets (\[\]), followed by the end of the string ($)
[A-Z]+\[\d+\][A-Z]+ the actual pattern - 1 or more digits in brackets surrounded by characters
$ end of string
I'd comment but I don't have the rep. Consider this an expansion on Jeroen's comment.
A length check and regex to match the bracketed integer won't necessarily guarantee a legitimate string. What if there are two bracketed integers? What if the 'letters' prior to the brackets contain an invalid character? I'd recommend a string length check followed by something like:
^[a-zA-Z]+\[[0-9]+\][a-zA-Z]+$
Which will further constrain the match. Add capturing groups as needed.
I think you should use string.Length and regex (^[a-zA-Z]+\[-?\d+\][a-zA-Z]+$) to test the completeness.
So i have the following RegEx for the purpose of finding and adding whitespace:
(\S)(\()
So for a string like "SomeText(Somemoretext)" I want to update this to "SomeText (Somemoretext)" it matches "t(" and so my replace eliminates the "t" from the string which is not good. I also do not know what the character could be, I'm merely trying to find the non-existence of whitespace.
Is there a better expression to use or is there a way to exclude the found character from the match returned so that I can safely replace without catching characters i do not want to replace?
Thanks
I find lookarounds hard to read and would prefer using substitutions in the replacement string instead:
var s = Regex.Replace("test1() test2()", #"(\S)\(", "$1 (");
Debug.Assert(s == "test1 () test2 ()");
$1 inserts the first capture group from the regex into the replacement string which is the non-space character before the opening parenthesis (.
If you need to detect the absence of space before a specific character (such as bracket) after a word, how about the following?
\b(?=[^\s])\(
This will detect words ( [a-zA-z0-9_] that are followed by a bracket, without a space).
(if I got your problem correctly) you can replace the full match with ( and get exactly what you need.
In case you need to look for absence spaces before a symbol (like a bracket) in any kind of text (as in the text may be non-word, such as punctuation) you might want to use the following instead.
^(?:\S*)(\()(?:\S*)$
When using this, your result will be in group 1, instead of just full match (which now contains the whole line, if a line is matched).
can anyone please help me to figure Regex attribute for string field.
I want my string should be in format of FirstName#LastName thats is it.. I require only one special char in between and rest all alphabets only..
You can use the expression [A-Za-z]+#[A-Za-z]+ to test against a nonempty string of alphabetical characters, followed by an # sign and again followed by a nonempty string of alphabetical characters. You can test it online here.
If you want to accept any non-alphanumeric characters in the middle, like $,#,_,- etc, you can use the following,
[a-zA-Z]+[^a-zA-Z\d\s][a-zA-Z]+
it will match all these among others,
FirstName#LastName
FirstName-LastName
FirstName_LastName
FirstName$LastName
FirstName:LastName
Live Demo
If you want to match whitespace in between as well then simply remove \s from above expression.
Hope it helps.
So I am trying to write a regex in c# (.NET) to match on a range of unicode characters that could potentially be found in a string. As a simple test, I attempted to match on a single unicode character, \u8221, which is the character ”. If I use the regex string "”", I get a match against my test string that contains this character. If, however, I change my regex to "\u8221", I don't get a match. Anyone know why this could be and how to get it to work? I have been pulling my hair out over this one. Thanks in advance.
You are not matching the correct character. \u requires a character code in hexadecimal. Try \u201D instead.
I need my C# regex to only match full words, and I need to make sure that +-/*() delimit words as well (I'm not sure if the last part is already set that way.) I find regexes very confusing and would like some help on the matter.
Currently, my regex is:
public Regex codeFunctions = new Regex("draw_line|draw_rectangle|draw_circle");
Thank you! :)
Try
public Regex codeFunctions = new Regex(#"\b(draw_line|draw_rectangle|draw_circle)\b");
The \b means match a word boundary, i.e. a transition from a non-word character to a word character (or vice versa).
Word characters include alphabet characters, digits, and the underscore symbol. Non-word characters include everything else, including +-/*(), so it should work fine for you.
See the Regex Class documentation for more details.
The # at the start of the string makes the string a verbatim string, otherwise you have to type two backslashes to make one backslash.
Do you want to match any words, or just the words listed above? To match an arbitrary word, substitute this for the bit that creates the Regex object:
new Regex (#"\b(\w+)\b");
In the future, if you want more characters to be treated as whitespace (for example, underscores), I would recommend String.Replace-ing them to a space character. There may be a clever way to get the same effect with regular expressions, but personally I think it would be too clever. The String.Replace version is obvious.
Also, I can't help but recommend that you read up on regular expressions. Yes, they look like line noise until you get used to them, but once you do they're convenient and there are plenty of good resources out there to help you.