I am trying to validate mobile using regular expression
so for i have tried
https://regex101.com/#javascript
My Expresion is ((\d[^0-5]|[6-9])\d{9})|^(\d)\1*$
i need validate moblie number like below
1.it should not start with 0-5
e.g 0600432102
2.not all the same or in sequence
e.g 1111111111 or 0123456789 or 9876543210
3.lenght is 10 digit
where i made error.
help me....
thanks ....
This covers all criteria and tests a few numbers. It does however not specify the reason for a number being invalid - I leave that to you.
var numArr = ["1111111111", "0123456789", "9876543210", "8682375827", "83255"];
for (var i = 0; i < numArr.length; i++) {
console.log(numArr[i] + " is " + validate(numArr[i]));
}
function validate(num) {
if ((num.match(/^[6-9]\d{9}$/) && (!num.match(/^([0-9])\1{9}$/))) && (!isIncr(num) && (!isDecr(num)))) {
return ( "Valid") ;
} else {
return ( "Not valid") ;
}
}
function isIncr(num) {
for (var i = 1; i < num.length; i++) {
if (num[i] == parseInt(num[i - 1]) + 1) {
continue;
} else {
return false;
}
}
return true;
}
function isDecr(num) {
for (var i = 1; i < num.length; i++) {
if (num[i] == parseInt(num[i - 1]) - 1) {
continue;
} else {
return false;
}
}
return true;
}
(([6-9])(?!\2{9})\d{9})
will:
Check if the number starts with 6-9. It stores this in match group 2.
Check that the first digit is not followed by exactly 9 (you can set the limits here) repetitions of the same digit.
Continues to find if there are 9 more digits after the first digit.
will not:
Check whether the numbers are ascending/descending order.
Check for patterns such as 6566666666
you can use the following regex:
/^(?!9876543210)(?!([\d])\1{9})[6-9][\d]{9}$/mg
Explanation
(?!9876543210) Check that the string is different (it's the only sequence possible)
(?!([\d])\1{9}) Check that this is not the same number repeated
[6-9][\d]{9} Check that the number start with 6 to 9, followed by 9 numbers
This regex works for repititions and numbers not starting with 0-5.
(?!([0-9])\1{9})(\b[6-9]\d{9})
See demo here: https://regex101.com/r/uJ0vD4/9
It doesn't detect ascending and descending numbers. You can achieve that using a loop in your program
Related
This is the homework spec
// Returns a string where the same characters next each other in
// string n are separated by "22"
//
// Pseudocode Example:
// doubleDouble("goodbye") => "go22odbye"
// doubleDouble("yyuu") => "y22yu22u"
// doubleDouble("aaaa") => "a22a22a22a"
//
string doubleDouble(string n)
{
return ""; // This is not always correct.
}
This is part of a homework set that deals with recursion. I know how to use recursion with an int function but I'm not entirely sure how to approach this problem when a string is passed through. Is it as simple as n.length == n.length() +1 ? and then simply insert "22" ? Alongside this, how would one traverse the string? Thanks!
I would say that the base case be if n turns out to be just a blank space, or if it has a size 0, then simply return the string back, no changes made.
This seems OK to me (untested code)
string doubleDouble(string n)
{
if (n.size() < 2)
return n;
else if (n[0] == n[1])
return n[0] + ("22" + doubleDouble(n.substr(1)));
else
return n[0] + doubleDouble(n.substr(1));
}
As you can see you recurse on substrings of the original string.
Undoubtedly there are more efficient ways to do this (that minimise the string copying) but I'll leave that for you.
PS you need to enable C++11 to get all the required versions of + for strings.
string doubleDouble(string n)
{
if(n.length() == 2){
if(n[0]==n[1])
return n.insert(1,"22");
else
return n;
}
else if(n.length() == 1)
return n;
else
if(n.length() % 2 == 1 || n[n.length()/2]!=n[n.length()/2+1])
return doubleDouble(n.substr(0,n.length() / 2)) + doubleDouble(n.substr((n.length() / 2),n.length()));
else if(n[n.length()/2]==n[n.length()/2+1])
return doubleDouble(n.substr(0,n.length() / 2)) +"22"+ doubleDouble(n.substr((n.length() / 2),n.length()));
}
My solution uses 'Divide-and-conquer' paradigm to separate the string in 2 halfs again and again until it finds 1 or 2 characters only. The solution above is simpler. I tried an academic style.
I need to check whether a word has exactly 6 letters and 1 digit or not.
I've tried this answer and modified my regex to look like this:
^[a-zA-Z]{6}\d{1}$ (meaning any letter between a-z or A-Z 6 times and 1 digit)
but I get no matches for any string that I could imagine (examples: sixsix1, 1sixsix, six1six)
I am using regex101 to compile the regex
What did I do wrong?
You may try this:
^(?=^[^\d]*\d{1}[^\d]*$)[a-zA-Z0-9]{7}$
demo
Explanation:
(?=^[^\d]*\d{1}[^\d]*$) ensures to look for just a single number
in the entire string.
[a-zA-Z0-9]{7} ensures there is 7 character within a-zA-Z0-9. As
previously it was confirmed that there is no more than 1 number thus
it will only match when it has 6 chars and one number.
Borrowing from this answer since I've never written Java:
String s = "six1six";
if (s.length() != 7) return false;
int digits = 0;
int letters = 0;
for (int i = 0; i < 7; i++) {
if (Character.isDigit(s.charAt(i))) {
digits++;
}
else if (Character.isLetter(s.charAt(i))) {
letters++;
}
}
return digits == 1 && letters == 6;
Is there really no LINQ equivalent in Java? this is hideous.
I have a file with XML statements as such:
<rdfs:label>(mild) (585.2)</rdfs:label>
How would I remove the text in between parantheses, not the numbers so the data would read:
<rdfs:label>(585.2)</rdfs:label>
The number in rdfs:label may be a whole number, a decimal of up to two places, or a range composed of whole numbers, decimals or a combination of both.
I've tried the following regex but it does not seem to be working correctly:
<rdfs:label>?([0-9]*)
\(\1
I just bit the bullet and wrote a java method to handle the irregular data.
The first capturing group of this regex takes the first set of parentheses and whatever is between them, with the following space.
<.*>(\(.*\)\s)
Here is the java method that I used to solve this issue
String methodName(String a){
for(int i = 0; i < a.length(); i++){
if(a.charAt(i) == '('){
if(Character.isDigit(a.charAt(i+1)) || (a.charAt(i+1) == 'V' && Character.isDigit(a.charAt(i+2))) || (a.charAt(i+1) == 'E' && Character.isDigit(a.charAt(i+2)))){
for(int v = i; v < a.length(); v++){
if(a.charAt(v) == ')'){
return(a.substring(i+1,v));
}
}
}
}
}
There is a strange string of 10 characters ether '0' or '1'. I have n filter strings each having 10 characters ether '0' or '1'. A '1' at the i-th position in a filter string means that if I applies this filter to the i-th character of the strange string, i-th character of strange string will be inverted: it becomes '1' if it was '0', and vice versa, whereas a '0' at the i-th position in a filter string will not do anything to the strange string. I can apply any number of filters. I can pick any number of filters and can apply to strange string. Now i want to find how many different subsets of all the filters can I apply to transform this strange string so that strange string will contain only 1's? I am not able to generalise the problem for any number of strings. Can anybody help.
Let us have some test cases
Enter strange string :1111111111
Total filter strings : 2
Enter filter strings :
0000000000
0000000000
Output is: 4
Explanation : Strange string is already having all characters 1's, and I have two different identity filters. I can either apply the empty subset of filters, the first filter only, the second filter only, or both.
Enter strange string :0101010101
Total filter strings : 3
Enter filter strings :
1010101010
1010000000
0000101010
Output is: 2
Explanation : I can either apply the first filter (and invert all 0's) or apply the second and third filters in any order.
Brute force algorithm:
std::uint16_t apply_filters(std::uint16_t init,
const std::vector<std::uint16_t>& filters,
const std::vector<bool>& mask)
{
auto res = init;
for (std::size_t i = 0; i != filters.size(); ++i) {
if (mask[i]) {
res ^= filters[i];
}
}
return res;
}
bool increase(std::vector<bool>& bs)
{
for (std::size_t i = 0; i != bs.size(); ++i) {
bs[i] = !bs[i];
if (bs[i] == true) {
return true;
}
}
return false; // overflow
}
std::size_t count_filters_combination(std::uint16_t init,
const std::vector<std::uint16_t>& filters)
{
std::vector<bool> bs(filters.size());
std::size_t count = 0;
const std::uint16_t expected = 0b1111111111;
do
{
if (expected == apply_filters(init, filters, bs)) {
++count;
}
} while (increase(bs));
return count;
}
Live Demo
I wrote my solution in python. It should be pretty easy to understand. I'll leave it to you to translate it to C++.
def filterCombinations(original, filters):
combinations = 1 if original == 0b1111111111 else 0
for i in xrange(len(filters)):
newvalue = original ^ filters[i]
newfilters = filters[i+1:]
combinations += filterCombinations(newvalue, newfilters)
return combinations
Using 3 filters, the first level of recursion looks like this:
filterCombinations(S, [F1, F2, F3])
--> X +
filterCombinations(S^F1, [F2, F3]) +
filterCombinations(S^F2, [F3]) +
filterCombinations(S^F3, [])
Where X is 1 if S == 1111111111 and 0 otherwise.
MFC, How do I check the CString format is match the IP Format,
For example User input
192.168,1,1 Error format
256.256.2.2 Error format
192.168.2 Error format
Some tip tell for me, thx
Search the string, using Find(), Mid() or Tokenize() and validate it's contents, based on a few required rules:
e.g.
no alpha characters
precisely 3 periods (.)
numbers between periods
0 <= number <= 255
first number > 0
etc...
OK, I find the solution
// precisely 3 periods (.), my ip is save if strCtrlIP
CString strCheck(strCtrlIP);
int nPointNum = 0;
nPointNum = strCheck.Remove('.');
if(nPointNum != 3)
{
AfxMessageBox(_T("IP example:192.168.0.1"));
return;
}
// numbers between periods, 0 <= number <= 255
strCheck.Format(_T("%s"), strCtrlIP);
while(strCheck.Find(_T(".")) >= 0)
{
int nLoc = strCheck.Find(_T("."));
int nVal = _ttoi(strCheck.Left(nLoc));
strCheck = strCheck.Right(strCheck.GetLength() - (nLoc+1)); // egnore point
if(nVal < nUserLimitDown || nVal > nUserLimitUp || strCheck.IsEmpty())
{
AfxMessageBox(_T("IP example:192.168.0.1"));
return;
}
}
if(_ttoi(strCheck) < nUserLimitDown || _ttoi(strCheck) > nUserLimitUp)
{
AfxMessageBox(_T("IP example:192.168.0.1"));
return;
}