I am in need of some regular expression help. (regex is not my strong point!) I have to iterate through a spreadsheet and match the strings that look like this:
AZP2006-056.03
ABC####-###.##
so first three can be any letter, four digit year and a 3 digit number with a two decimal place value.
Can someone please help me with te regular expression to match this case?
Thanks!
[A-Z]{3}\d{4}-\d{3}\.\d{2}
I'm not sure if you have to escape the hyphen in -\d{3}
\w{3}\d{4}-\d{3}\.\d{2}
This is a pretty simple regex, even if you're new to it a few minutes of reading documentation and trying things out should be enough to come up with something that works. I'd suggest using a tool like this to interactively see what your pattern is matching:
http://gskinner.com/RegExr/
This should do the trick.
\w{3}\d{4}-\d{3}\.\d{2}
I think this should work: [a-zA-Z]{3}[0-9]{4}-[0-9]{3}.[0-9]{2}
I know it's pretty late to answer this now, but I do note that you said the 4 digit number following the letters was to be the year.
To avoid getting something like ABC9786-654.43, it would also be good to validate the year so I would use:
(?#better version - validates year)^[A-Z]{3}(?<year>((19)[0-9]{2})|((20)[01][15]))-\d{3}\.\d{2}$
Related
I'm trying to create a regular expression that would match files of this pattern:
Id_Name_processID_timestamp_logName.txt
Example of filename: abcd_Service_11234_15112013_Log.txt
I don't need perfect matching something that would match anything_anything_anything_anything_anything.txt would work for me.
I haven't tried anything just lost time starring at this Regex Tutorial for quite a long time, i don t know where to start :(.
Go to this site: http://regexpal.com/
Put abcd_Service_11234_15112013_Log.txt in the lower box.
Start writing your rexex on the top box, until it matches (it's a simple one, really, chars, underscore, rinse and repeat) ... You'll be ok ...
My regex, a short simple one.
^\w+_\w+.txt
Edit:
I do agree with the 1st answer: You really need to try something on your own but that website must be the least userfriendly page on regex. You get my answer out of sympathy ;)
I've been working on my own simple Wikipedia parser in C#. So far I'm not getting very far because of this problem.
I extract the string {e|24} but it could contain any number. All I want to do is simply extract the number from this string.
This is the code I am using currently:
Match num = Regex.Match(exp.Value, "[0-9]*");
Console.WriteLine(num.Value);
However num.Value is blank.
Can someone please explain why this is not working and how I can fix it?
You would want to use [0-9]+ to ensure at least one number. [0-9]* allows it to be matched 0 times or more, thus getting blanks
My suggestion, make the regexp: \d+
Works. Simpler. Shorter, uses no groups or ranges.
I have some troubles making this regexp:
I simply want the regexp test to fail if the input contains this symbol "<" directly followed by a letter, i.e something like: <[^a-zA-Z]
But I want it to work even if the "<" is not found. How can I do that?
EDIT: some examples
<Wrong example
Wrong <Example
Good Example
< Good Example
Good < Example
Good< Example
Good Example<
EDIT 2:
When working with asp.net, you can't send a form with this text in an input for example:
<Previous
EDIT 3: This regular expression will be passed in a control that I cannot change, and it works by validating the input with a regular expression. Therefore I cannot match for the bad input
A negative lookahead regex on its own like
^(?!.*<[a-zA-Z])
would check that a letter never followed a left angle bracket, but an empty string would match your criteria. Do you also need to make sure it contains at least one alpha, like this?
^(?!.*<[a-zA-Z]).*[a-zA-Z]
In Perl:
while (<DATA>) {
print if /^(?!.*<[a-zA-Z])/;
}
__DATA__
<Wrong example
Wrong <Example
Good Example
< Good Example
Good < Example
Good< Example
Good Example<
OUTPUT
Good Example
< Good Example
Good < Example
Good< Example
Good Example<
It would probably be simpler to match for the bad input and blacklist on it instead of matching on good input and whitelisting it.
Reject any input that matches the following regular expression:
<[a-zA-Z]
If you REALLY need a whitelisting solution (because you don't control the actual validation logic, only the regex), you could do this:
^(?:[^<]|<[^a-zA-Z]|<$)*$
(You can change the last Kleene star to a plus if you also want to make sure the input is nonempty.)
You can use [a-zA-Z]*(<[^a-zA-Z])?[a-zA-Z]*.
EDIT: Perhaps not the most ideal pattern, but it matches your "good" examples and does not match your "wrong" examples:
^((^|(<[^a-zA-Z]|[^<a-zA-Z])+)[a-zA-Z]+)+(<[^a-zA-Z]*|[^<a-zA-Z]*)$
I think you want to use parenthesis followed by a ? to indicate zero or one occurrences of that pattern:
(<[^a-zA-Z])?
I'm assuming that you are building a larger regex pattern, and is just a part of it.
I have a text box which accepts CVV code of 3 or 4 numbers and im using regular expression validator
this is wat im using : is this correct way of using or not
code : ValidationExpression="(\d{3,4}$"
Can anyone help me on this
Regards,
smartdev
That one should, to my opinion generate an error because you are not closing the group capture, at least that is what I think.
\d{3,4} should do the trick
\d{3,4} means that you are matching a minimum of three and a maximum of 4 digits. Maybe this introductory tutorial will do the trick.
I need a regular expression that will not let someone continue to check out if the type in the word 'box' or 'Box' or 'BOX' followed by numbers. Both conditions have to be met for this to work, the word box and the numbers not one or the other. Does anyone know how to write this?
EDIT:
Hey guys, I've tried a few and the problem is that it's preventing regular addresses like 1234 S. Something Lane. All I want to prevent is if the user types in something like p.o. box 1234. We're figuring anything with the word 'box' and numbers after it should not be accepted.
thanks
Edited after the question was clarified:
new Regex(#"\bbox\s*\d+", RegexOptions.IgnoreCase);
That will match "box" as a word by itself, optionally followed by whitespace, followed by a number, anywhere in the input string. So it will match:
box12
Box 12
P.O. Box 12
BOX 12a
It won't match:
boxtree 12
boombox 12
Box Pog
Box Pog 12
(It would have been very helpful if your original question had explained what you were actually trying to do!)
(box|Box|BOX)[0-9]+
EDIT after clarification of question:
I guess you actually want a case-insensitive search on:
box\s*\d+
\s*: Any amount of whitespace
\d+: At least one number
You can define the number of digits, if you want to. Example for 3-5 digits:
box\s*\d{3,5}
At least in my experience it's easier to write a RegEx to match the expected case strictly, rather than trying to exclude an exceptional case.
If you show an example of the format you're wanting to accept it will be easier for people to answer this.
If you're wanting to prefix a string with any variant of "Box" and then with numbers, something like this should work (make sure you use RegexOptions.IgnoreCase)
^box\d+$
Which would match for ...
box1
BOX1234567
bOx3
...etc...
have you tried something like
/box\d+/
plus IgnoreCase?
Don't forget the "post office" option.
^(p(ost|.) *o(ffice|.) )?box *[0-9]+$
this :
box|Box|BOX\d+
will match these specific cases box,Box,BOX and all these followed by numbers but it will also return true in this case for instance :
asBox123asdasd
If you want to get true if the string is exactly box123 for instance you should use :
^box|Box|BOX\d+$
instead