data annonation regular expression to check digit between specify numbers [closed] - regex

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How to write a regular expression which contain format like 00:00
the 00 before : must be digit between 0-24 and 00 after : must be digit between 0-59
i have my code below but somehow some it cannot work properly.
[RegularExpression(#"[0-24]+:[0-59]", ErrorMessage = "Format was invalid")]
For Exmaple
00:59 was accepted
25:60 was not accepted

What about something simple like this?
[RegularExpression(#"(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])", ErrorMessage = "Format was invalid")]
It matches on any time from 00:00 to 23:59. Note that it does not need the leading zero as written, so will also accept times like 7:00 or 7:3 for three minutes past seven (this is consistent with how you asked your question).
If you want it to require the leading zeros, just remove the question marks...

try this "([0-9]{1,2}|100):(1?[0-9]{1,2}|200)"

try with this: \b(?:2[0-4]|1?\d):(?:5\d|[1-4]?\d)\b
with leading zero:
\b(?:2[0-4]|1\d|0?\d):(?:5\d|[1-4]\d|0?\d)\b

Why do you need to solve every problem by REGEX.You should not complicate things.Break down the code.This would make your code more readable and fluent.
The method below yields the perfect matches.
public IEnumerable<string> getValues(string s)//this method returns the valid matches from s
{
Regex r=new Regex(#"\d{1,}:\d{1,}");
foreach(Match m in r.Matches(s))
{
string[] str=m.Value.Split(':');
if((int.Parse(str[0])>=0 && int.Parse(str[0])<=24) && (int.Parse(str[1])>=0&&int.Parse(str[1])<=59))
yield return str[0]+":"+str[1];
}
}
Use the above method by using a for-each statement!
foreach(string s in getValues("5:16 100:200"))//getValues method yields the perfect match
Console.WriteLine(s);//5:16

Related

String Encode in C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I need to convert the string "Test €" to "Test &#8364 ;" and vice versa. Please make a note that its a string and not from the xml. For your information I am developing the application in C++ and using Xerces also for XML Parsing. Please help me how it can be achieved in c++ application.
Thanks,
Ram
I think this answer may be platform-dependent, though I don't know for sure.
You can use stringstreams and casting. If lookup is a string holding the decimal version of the character code, this function will return the character version:
char fixchar(string lookup){
stringstream converter (lookup);
int i;
converter >> dec >> i;
return (char)i
(Note that for hex strings, which are prefixed with #x instead of #, you can just use hex instead of dec).
You can get the lookup strings by using the find function on the original string. Here's a loop that uses the above function to convert a string (called fixd) with &#x[number] substrings into a normal string with no character codes:
while (fixd.find("&#x")!=string::npos){
tag = int(fixd.find("&#"));
endtag = int(fixd.find(";"));
fixd = fixd.substr(0,tag) + fixchar(fixd.substr(tag+3,endtag-tag-3)) + fixd.substr(endtag+1, fixd.length()-tag-4);
}
Similarly, you should be able to get the int version of a character just by casting it, after which you can do whatever you want with it, including adding it in decimal form to a string.

Print a string store into the stack - Tricky challenge [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I found following C++ source code:
void victory()
{
printf("xxxxxxxxxxxxxxxxxxxxxxxx"); //random string
}
int main()
{
if(0) victory();
/* code here! */
}
Your main objective is to print the random string(from function victory) on stdout
How can It possible? How can I print the random string? It's a tricky traps, just for curiosity. Above all, I can't use pointers...
Rules:
Max 12 chars.
You can't use: "main", "victory", "asm", "%", "*", "_", "#", "/", "&".
You have only one semicolon.
It is pretty clear that there is no way to solve this in any remotely standards-compliant way.
Since this is being framed as a "security" question, this suggest that it's OK to use compiler- and OS-specific hacks. With this in mind, the following works on my system using gcc:
#include <stdio.h>
void victory()
{
printf("SxxxxxxxxxxxxxxxxxxxxxxE"); //random string
}
int main()
{
if(0) victory();
puts(""-25);
}
It relies on the fact that the "" gets placed by the compiler immediately after the string literal that we're trying to recover. Here, 25 is the (known) length of the unknown string.
Of course, this has UNDEFINED BEHAVIOUR written all over it in huge neon letters. Handle with care.

How to tell a C++ regex to be treated as plain text / escape all characters [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am wrting a c++ function to implement string replacement. The function is like:
using namespace std;
string Replace(const string & str, const string & strOld,
const string & strNew, bool useRegex, bool caseSensitive)
{
regex::flag_type flag=regex::basic;
if(!caseSensitive)
flag |= regex::icase;
if(!useRegex)
// WHAT TO DO ?
std::regex rx(strOld,flag);
string output=regex_replace(str,rx,strNew);
return output;
}
It replaces all occurrences of strOld in str to strNew. I attempted to use std::regex and std::regex_replace to implement it. It works well in case useRegex is true. However, in case useRegex is false, I am not able to tell them that strOld is just a plain string instead of a regex string.
For example, When I call:
string output=Replace("hello.",".","?",false,true);
It returns "??????" while I expect it to be "hello?".
Halfway-solution is to pre-process the regex and escape all metacharacters manually. It's the best solution if this feature is missing from C++11 which from comments sounds like it is.

Search for all substings between "+" and "n" characters [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm trying to change some equations from input file to a "readable" form.
Currently I need to read all numerical values (as substrings, will convert them to int later) within a string P. All the values I'm interested with are between "+" and "n" characters (in this order for one loop, and the inverted order for other loop).
I need a loop which finds all of them and save them to array (of unknown size, since I don't know how long the string P will be).
Input examples (strings P in quotes):
"+n2+-n"
First loop (reads from + to n) so substrings C are: "", "-"
The second loop (reads from n to +) so substrings E are: "2", ""
"+2n3+3n2+n"
First loop: "2", "3", ""
Second loop: "3", "2", ""
"+-n14+-11n+1"
First loop: "-", "-11"
Second loop: "14", ""
I could add "+" to the end of the P string if solution requires.
ps. If someone's have an idea how to extract a constant from the end of string seen in example 3 (+1, or any other) I would really appreciate. The hard thing is I cannot tell how long it'll be (can by +1 can be -300000 or so).
Please consider to take a look at regular expressions (in general) and the new std::regex class from c++0x (in particular).
C++0x: Regular Expressions
Regular expressions are always a elegant solution if you want to parse any more complex patterns.
I didn't really understood your question, but what I personally use whenever i have unknown-length / extremely big inputs is a linked list , that you could implement in C++ with dynamic memory allocation , which is also possible in C with malloc(), if you ever find that you need C syntax.
As with the parsing of the input, you can use a variable initialized with 1, and you just multiply it with -1 every time you get to the end of a substring, and use simple if statement that covers each case. Or you could use a char that takes either "n" or "+", and so you have to make the if statement cover only a few lines of code, after you're done with the actual parsing, and whenever your cursor gets to the above-mentioned char variable, it acts like it's done it's job for another substring.

Find function doesn't return the right value [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Is it not possible to fill a map like this:
void Spel::Fill( void )
{
int buildslist[] = {3,3,2,2,2,2,3,2,2,2,2,3,3,2,2,2,2,1,1,1,1,1};
Building::buildings p;
for( int i = Building::INDIGOK; i < Building::STADSHUIS; i++)
{
p = (Building::buildings) i;
gebouwenMap[p] = buildslist[i];
}
}
This gives all 0. Building::buildings is an enum with some building names. The buildslist is a list of how many people could join that building.
First, there is not enough code to give an actual answer. Please improve your question so that we can help you properly.
Second, the title is misleading "Find function doesn't return the right value". There is no "find" function and there is no "return value" because the only function you show returns void.
Now thet this is sorted out, let me try to help you:
p = (Building::buildings) i;
This is a cast from int to enum. I think this is bad C++ (probably undefined as in might work for some compiler but not as a rule of thumb). You would have to use a switch here I think.
Please write code in English. Do you imagine if someone you work with is Japanese and would write the code with japanese variable names? Even if the project is in Dutch, write code in English including comments.
EDIT: You might want to use strings instead of an enum here.
Try using a std::map<std::string, int> to encode your building names instead of an enum, then use an std::map::iterator to iterate through it.