I have got this regexp "^[0-9]+\.?[0-9]*$") to match a double number or an integer number in visual C++ but it doesn't seem to work. Any ideas?
This is how I am applying the code:
if (System::Text::RegularExpressions::Regex::IsMatch(e0, "^[0-9]+\.?[0-9]*$")){
e0_val = System::Convert::ToDouble(e0);
}
the regexp above is not perfect since it accepts "09" which is not a valid number. a better expression would be:
"^(-?)(0|([1-9][0-9]*))(\\.[0-9]+)?$"
where:
1. is an optional negative sign;
2. is zero or a valid non-zero integer;
4. is the optional fracture part;
in theory, the fracture part should be written as "(\.[0-9]*[1-9])?"
instead, because a number must not have tailing zeroes. in practice, the source string might have been created with a fixed number of digits e.g:
printf("%.1f", x);
so it might easily end with a zero character. and, of course, these are all fixed point representations, not the doubles themselves. a double number can
be written as -1.23e-4 as well instead of -0.000123.
There's nothing wrong with the regex per se, it's your escaping that's at fault. You need to double escape the \ character since that's also a C++ string escape character.
Additionaly there is an edge case where this regex would think that 1. is a valid floating pointer number. So you might be better off with /^[0-9]+(\\.[0-9]+)?$ which eliminates that possibility.
Maybe not a direct answer, just useful information. The regexp:
std::regex rx(R"(^([+-]?(?:[[:d:]]+\.?|[[:d:]]*\.[[:d:]]+))(?:[Ee][+-]?[[:d:]]+)?$)");
matches strings:
"1", "0", "10",
"1000.1", "+1",
"+10", "-10", "1.",
".1", "1.1", "+1.",
"-1.", "+.1", "-.1",
"009", "+009", "-009",
"-01e0", "+01E0", "+1e-1",
"+1e+1", "+1.e1", "1E1",
"1E+1", "0.001e-12", "0.111111111111111"
and does not matches the next strings:
".", "1a", "++1",
"+-1", "+", "-.",
"-", "--1.", "1.e.1",
"1e.1", "0+.e0"
The first ones look like valid values for the double type in C++, e.g. double test = +009.e+10 is OK.
Play it in ideone.com: https://ideone.com/ooF8sG
/^[0-9]+.[0-9]+$ : use this for doubles.
accepts 123.123 types.
/^[0-9]*[.]?[0-9]+$/
Regex above works for doubles such as "45.5", "12", ".12"
(\d+)?\.(\d+)?
Regex above works for doubles such as "45.5", "12.", ".12"
Related
Let's say I have a string of 2 characters. Using regex (as a thought exercise), I want to accept it only if the first character has an ascii value bigger than that of the second character.
ae should not match because a is before e in the the ascii table.
ea, za and aA should match for the opposite reason
f$ should match because $ is before letters in the ascii table.
It doesn't matter if aa or a matches or not, I'm only interested in the base case. Any flavor of regex is allowed.
Can it be done ? What if we restrict the problem to lowercase letters only ? What if we restrict it to [abc] only ? What if we invert the condition (accept when the characters are ordered from smallest to biggest) ? What if I want it to work for N characters instead of 2 ?
I guess that'd be almost impossible for me to do it then, however bobble-bubble impressively solved the problem with:
^~*\}*\|*\{*z*y*x*w*v*u*t*s*r*q*p*o*n*m*l*k*j*i*h*g*f*e*d*c*b*a*`*_*\^*\]*\\*\[*Z*Y*X*W*V*U*T*S*R*Q*P*O*N*M*L*K*J*I*H*G*F*E*D*C*B*A*#*\?*\>*\=*\<*;*\:*9*8*7*6*5*4*3*2*1*0*\/*\.*\-*,*\+*\**\)*\(*'*&*%*\$*\#*"*\!*$(?!^)
bobble bubble RegEx Demo
Maybe for abc only or some short sequences we would approach solving the problem with some expression similar to,
^(abc|ab|ac|bc|a|b|c)$
^(?:abc|ab|ac|bc|a|b|c)$
that might help you to see how you would go about it.
RegEx Demo 1
You can simplify that to:
^(a?b?c?)$
^(?:a?b?c?)$
RegEx Demo 2
but I'm not so sure about it.
The number of chars you're trying to allow is irrelevant to the problem you are trying to solve:
because you can simply add an independent statement, if you will, for that, such as with:
(?!.{n})
where n-1 would be the number of chars allowed, which in this case would be
(?!.{3})^(?:a?b?c?)$
(?!.{3})^(a?b?c?)$
RegEx Demo 3
A regex is not the best tool for the job.
But it's doable. A naive approach is to enumerate all the printable ascii characters and their corresponding lower range:
\x21[ -\x20]|\x22[ -\x21]|\x23[ -\x22]|\x24[ -\x23]|\x25[ -\x24]|\x26[ -\x25]|\x27[ -\x26]|\x28[ -\x27]|\x29[ -\x28]|\x2a[ -\x29]|\x2b[ -\x2a]|\x2c[ -\x2b]|\x2d[ -\x2c]|\x2e[ -\x2d]|\x2f[ -\x2e]|\x30[ -\x2f]|\x31[ -\x30]|\x32[ -\x31]|\x33[ -\x32]|\x34[ -\x33]|\x35[ -\x34]|\x36[ -\x35]|\x37[ -\x36]|\x38[ -\x37]|\x39[ -\x38]|\x3a[ -\x39]|\x3b[ -\x3a]|\x3c[ -\x3b]|\x3d[ -\x3c]|\x3e[ -\x3d]|\x3f[ -\x3e]|\x40[ -\x3f]|\x41[ -\x40]|\x42[ -\x41]|\x43[ -\x42]|\x44[ -\x43]|\x45[ -\x44]|\x46[ -\x45]|\x47[ -\x46]|\x48[ -\x47]|\x49[ -\x48]|\x4a[ -\x49]|\x4b[ -\x4a]|\x4c[ -\x4b]|\x4d[ -\x4c]|\x4e[ -\x4d]|\x4f[ -\x4e]|\x50[ -\x4f]|\x51[ -\x50]|\x52[ -\x51]|\x53[ -\x52]|\x54[ -\x53]|\x55[ -\x54]|\x56[ -\x55]|\x57[ -\x56]|\x58[ -\x57]|\x59[ -\x58]|\x5a[ -\x59]|\x5b[ -\x5a]|\x5c[ -\x5b]|\x5d[ -\x5c]|\x5e[ -\x5d]|\x5f[ -\x5e]|\x60[ -\x5f]|\x61[ -\x60]|\x62[ -\x61]|\x63[ -\x62]|\x64[ -\x63]|\x65[ -\x64]|\x66[ -\x65]|\x67[ -\x66]|\x68[ -\x67]|\x69[ -\x68]|\x6a[ -\x69]|\x6b[ -\x6a]|\x6c[ -\x6b]|\x6d[ -\x6c]|\x6e[ -\x6d]|\x6f[ -\x6e]|\x70[ -\x6f]|\x71[ -\x70]|\x72[ -\x71]|\x73[ -\x72]|\x74[ -\x73]|\x75[ -\x74]|\x76[ -\x75]|\x77[ -\x76]|\x78[ -\x77]|\x79[ -\x78]|\x7a[ -\x79]|\x7b[ -\x7a]|\x7c[ -\x7b]|\x7d[ -\x7c]|\x7e[ -\x7d]|\x7f[ -\x7e]
Try it online!
A (better) alternative is to enumerate the ascii characters in reverse order and use the ^ and $ anchors to assert there is nothing else unmatched. This should work for any string length:
^\x7f?\x7e?\x7d?\x7c?\x7b?z?y?x?w?v?u?t?s?r?q?p?o?n?m?l?k?j?i?h?g?f?e?d?c?b?a?`?\x5f?\x5e?\x5d?\x5c?\x5b?Z?Y?X?W?V?U?T?S?R?Q?P?O?N?M?L?K?J?I?H?G?F?E?D?C?B?A?#?\x3f?\x3e?\x3d?\x3c?\x3b?\x3a?9?8?7?6?5?4?3?2?1?0?\x2f?\x2e?\x2d?\x2c?\x2b?\x2a?\x29?\x28?\x27?\x26?\x25?\x24?\x23?\x22?\x21?\x20?$
Try it online!
You may replace ? with * if you want to allow duplicate characters.
ps: some people can come up with absurdly long regexes when they aren't the right tool for the job: to parse email, html or the present question.
What is the regular expression that generates the language where every odd position in the string is an a? (Please answer with the shortest possible regex: minimal parentheses, no spaces and any piped strings in alphabetical order!)
I assume I'm working with only a's and b's.
(a(a|b))+ would only cover even strings: a(a|b), a(a|b)a(a|b), etc.
How do I also cover the case that the string is odd? ex: a(a|b)a
Note: not using programming syntax
Edit: some valid strings would be: a, aa, aaa, aaaa, aaaaa, ab, aba, abab, ababa, etc.
EDIT: Solution
My instructor gave the answer (aa|ab)*. This is incorrect because it misses case(s), for example "a".
I think this suits your requirement:
^a(.a)*.?$
Position 1 must be "a": ^a
Repetitions of any character + a, making a sequence where odds are "a"'s: (.a)*
Allowing for a termination not ending in "a", ex abab: .?$
You can check it here: regex101
^(a.)*a?$
Allows empty values ("")
From start to end of line (^...$)
Every odd place (1,3,5,...) is an a followed by any letter/number
There may or may not be an a in the end
One sign shorter thay Jorge's answer, but allows empty values
See regex101 example here
I think this might help you
Case 1: even length strings (1(0+1))*
Case 2: odd length strings 1((0+1)1)*
Finally the answer is Case 1 + Case 2
I want to check, if given string is in this format:
Substring of \w chars can be delimited (not start or end) by , - or .. And there can be more than 2 delimiters. For example weerwer, as-sas.a are valid. -assa, a-s a-s, asd#d are not. For that I use ^\w+([ .-]\w+){0,2}$. Seems to work.
Whole string, that matches regexp above, I want to restrict to length of 8. For example asd, asd-asd are valid. asd-asd-asd is not. And that is my question. How to do that?
This is only in case, the solution depends also in another restriction I need. I need the substrings passed the above two to be able to be delimited by /. For example asd-asd/asd.asd/asdasd/asd asd/aaaaaa is valid. Pretty much same pattern as in 1. but not restricted to number of delimiters. I put it here only in case the 2. depends on it.
To close it here, I'm posting solution based on #sareed`s comment. Thank you.
I changed some things...
1. \w was changed to alphabetic and decimal numbers. Also _ delimiter was added.
2. Length of substring above was restricted to 16.
3. Number of / delimiters was restricted to 6.
Pattern:
\A(?=.{1,16}(\z|/))(((\p{Alphabetic}|\p{Numeric_Type=Decimal})+)([\. _-]((\p{Alphabetic}|\p{Numeric_Type=Decimal})+)){0,2})(/(?=.{1,16}(\z|/))(((\p{Alphabetic}|\p{Numeric_Type=Decimal})+)([\. _-]((\p{Alphabetic}|\p{Numeric_Type=Decimal})+)){0,2})){0,6}\z
It's not pretty but should work:
/^((?=[^/]{1,8}(\/|$))\w+([ .\-]\w+)*)(\/(?=[^/]{1,8}(\/|$))\w+([ .\-]\w+)*)*$/
Probably a very basic question but its buggging me that i can't easily find a solution...so i thought i should come to the wisdom of the SO wise ones...
I would like to be able to return a TRUE or FALSE acording to if a character string is a pure number rather than just containing numbers... The closest I got was
grepl("[0-9]","99393")
grepl("[0-9]","blah")
However this doesn't work since the following is returned as TRUE when it should be FALSE
grepl("[0-9]","993T3")
As ever any help would be appreciated!
EDIT
As joran pointed out it is important to note that the character string will only ever include integers and letters, i.e. will not include decimal points or commas for the number...
You should specify the whole regular expression and specify the beginning (^) and the end of the string ($). For instance :
> grepl("^[[:digit:]]+$","993T3")
[1] FALSE
Look at http://en.wikibooks.org/wiki/R_Programming/Text_Processing#Regular_Expressions if you want to learn more about regexp.
Why don't you just use robust internal methods for coercing to either an integer or numeric?
It will return NA if it can't be done. Use is.na if you want a logical result:
is.na( as.integer( "993T3" ) )
# [1] TRUE
is.na( as.integer( "99393" ) )
# [1] FALSE
Remember that if you are dealing with floating point numbers use as.numeric otherwise you will truncate the floating point part of your number using as.integer
What about !grepl("[^0-9]","993T3")?
Edit: This returns TRUE for the empty string. To avoid this, use
!grepl("[^0-9]", x) & nzchar(x)
for a vector x of character type.
Is it possible to write a regular expression that matches all strings that does not only contain numbers? If we have these strings:
abc
a4c
4bc
ab4
123
It should match the four first, but not the last one. I have tried fiddling around in RegexBuddy with lookaheads and stuff, but I can't seem to figure it out.
(?!^\d+$)^.+$
This says lookahead for lines that do not contain all digits and match the entire line.
Unless I am missing something, I think the most concise regex is...
/\D/
...or in other words, is there a not-digit in the string?
jjnguy had it correct (if slightly redundant) in an earlier revision.
.*?[^0-9].*
#Chad, your regex,
\b.*[a-zA-Z]+.*\b
should probably allow for non letters (eg, punctuation) even though Svish's examples didn't include one. Svish's primary requirement was: not all be digits.
\b.*[^0-9]+.*\b
Then, you don't need the + in there since all you need is to guarantee 1 non-digit is in there (more might be in there as covered by the .* on the ends).
\b.*[^0-9].*\b
Next, you can do away with the \b on either end since these are unnecessary constraints (invoking reference to alphanum and _).
.*[^0-9].*
Finally, note that this last regex shows that the problem can be solved with just the basics, those basics which have existed for decades (eg, no need for the look-ahead feature). In English, the question was logically equivalent to simply asking that 1 counter-example character be found within a string.
We can test this regex in a browser by copying the following into the location bar, replacing the string "6576576i7567" with whatever you want to test.
javascript:alert(new String("6576576i7567").match(".*[^0-9].*"));
/^\d*[a-z][a-z\d]*$/
Or, case insensitive version:
/^\d*[a-z][a-z\d]*$/i
May be a digit at the beginning, then at least one letter, then letters or digits
Try this:
/^.*\D+.*$/
It returns true if there is any simbol, that is not a number. Works fine with all languages.
Since you said "match", not just validate, the following regex will match correctly
\b.*[a-zA-Z]+.*\b
Passing Tests:
abc
a4c
4bc
ab4
1b1
11b
b11
Failing Tests:
123
if you are trying to match worlds that have at least one letter but they are formed by numbers and letters (or just letters), this is what I have used:
(\d*[a-zA-Z]+\d*)+
If we want to restrict valid characters so that string can be made from a limited set of characters, try this:
(?!^\d+$)^[a-zA-Z0-9_-]{3,}$
or
(?!^\d+$)^[\w-]{3,}$
/\w+/:
Matches any letter, number or underscore. any word character
.*[^0-9]{1,}.*
Works fine for us.
We want to use the used answer, but it's not working within YANG model.
And the one I provided here is easy to understand and it's clear:
start and end could be any chars, but, but there must be at least one NON NUMERICAL characters, which is greatest.
I am using /^[0-9]*$/gm in my JavaScript code to see if string is only numbers. If yes then it should fail otherwise it will return the string.
Below is working code snippet with test cases:
function isValidURL(string) {
var res = string.match(/^[0-9]*$/gm);
if (res == null)
return string;
else
return "fail";
};
var testCase1 = "abc";
console.log(isValidURL(testCase1)); // abc
var testCase2 = "a4c";
console.log(isValidURL(testCase2)); // a4c
var testCase3 = "4bc";
console.log(isValidURL(testCase3)); // 4bc
var testCase4 = "ab4";
console.log(isValidURL(testCase4)); // ab4
var testCase5 = "123"; // fail here
console.log(isValidURL(testCase5));
I had to do something similar in MySQL and the following whilst over simplified seems to have worked for me:
where fieldname regexp ^[a-zA-Z0-9]+$
and fieldname NOT REGEXP ^[0-9]+$
This shows all fields that are alphabetical and alphanumeric but any fields that are just numeric are hidden. This seems to work.
example:
name1 - Displayed
name - Displayed
name2 - Displayed
name3 - Displayed
name4 - Displayed
n4ame - Displayed
324234234 - Not Displayed