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.
Related
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.
Im trying to create a regex that will match ascii characters in a string so that they be converted with hex afterwards. The string is received as follows:<<<441234567895,ASCII,4,54657379>>> so I am looking to match everything between the third comma and the >>> characters at the end of the string like so.
<<<441234567895,ASCII,4,54657379>>>
So far I have managed to create this regex (/([^,]*,[^,]*)*([^;]*)>>>/) for it but the third comma is picked up as well which I don't want. What do I need to do to remove it from the match?
thanks Callum
(?<=,)[^,]+(?=>>>)
This should do it.See demo.
https://regex101.com/r/sJ9gM7/79
Do you need to use Regex?
string input = "<<<441234567895,ASCII,4,54657379>>>";
string match = input.Substring(3, input.Length - 6).Split(',')[3];
You can also use further splits on the beginning and ending padding strings or check their lengths if you want something safer than the Substring magic.
I am trying to create a regex to validate a string. The string could be of the following formats (to give an idea of what I am trying to do here):
145/1/3 or
748/57676/6765/454/345 or
45/234 45/235 45/236
So basically the string can contain numbers, spaces and forward slashes and the string can end with a number only. I am new at regex and have gone through many of the questions on the website. But please you have to admit that this is really confusing and difficult to master. And if someone could refer an author or any weblink that can teach regex, that would be really helpful. Thanks in advance mates!
I came up with this
^[0-9]( |[0-9]|\/)*[0-9]$
And used this to test it.
You can see it matches anything that begins (^) with a number, has zero or more (*) of either a space, a number or (|) a forward-slash (/) and ends ($) with a number.
Now that I am aware that the space and / cannot go together and multiple spaces and/or slashes are also not allowed, this RegEx is a better fit for you.
^[0-9]+([ \/][0-9]+)*$
This should work: ^[/\d\s]*\d$.
It is looking for the beginning of the string ^ , then 0 or more digits, spaces [/\d\s]* followed by a digit \d then the end of the string $.
You should use following regular expression:
(\d+(/\d+)*\s*)+
This mean: some digits (\d+) followed by optional repeating pattern of some digits and \ ((/\d+)*) followed by an optional number of whitespaces (\s*), all repeated at least once.
Try this:
^\d(\d|\s|\/)*\d$
\d = digit character (you can also use [0-9]).
\s = space character
The brackets followed by a star means to repeat a \d, \s, or / an infinite amount of times.
The final \d$ means the ending must match a digit.
I created the following regex expression for my C# file. Bascily I want the user's input to only be regular characters (A-Z lower or upper) and numbers. (spaces or symbols ).
[a-zA-Z0-9]
For some reason it only fails when its a symbol on its own. if theres characters mixed with it then the expression passes.
I can show you my code of how I implment it but I think its my expression.
Thanks!
The problem is that it can match anywhere. You need anchors:
^[a-zA-Z0-9]+\z
^ matches the start of a string, and \z matches the end of a string.
(Note: in .NET regex, $ matches the end of a string with an optional newline.)
This is because it will match any character in the string you need the following.
Forces it to match the entire string not just part of it
^[0-9a-zA-Z]*$
That regex will match every single alphanumeric character in the string as separate matches.
If you want to make sure the whole string the user entered only has alphanumeric characters you need to do something like:
^[a-zA-Z0-9]+$
Are you making sure to check the whole string? That is are you using an expression like
^[a-zA-Z0-9]*$
where ^ means the start of the string and $ means the end of the string?
example strings
785*()&!~`a
##$%$~2343
455frt&*&*
i want to capture the first and the third but not the second since it doesnt contain any alphabet character plz help
In fact, I think [a-zA-Z] might suffice to match your strings.
To capture the whole thing, try: ^.*[a-zA-Z].*$
Here is one possible way:
.*[a-zA-Z]+
You should maybe clarify a bit what you mean by 'catpuring': do you want the whole string of just the ascii bits?
Also, you don't say if it should match just plain Roman alphabet (A to Z) or if it should also match Unicode chars to match strings in other languages.
If you just need to test your string, in C# you would do:
bool matching = Regex.IsMatch(myString, "[a-zA-Z]");
You wouldn't need anything else, since just one letter anywhere in the myString string will match (according to your definition).
This is my favorite RegEx testing site: Javascript Regexp Tester and Cheat Sheet
If you want to match all letters (including non-ascii ones), use p{L} instead of [a-zA-Z]. See Unicode categories.