Word has exact X letters and Y digits - regex

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.

Related

my run-length encoding doesn't work with big numbers

I have a assingment were I need to code and decode txt files, for example: hello how are you? has to be coded as hel2o how are you? and aaaaaaaaaajkle as a10jkle.
while ( ! invoer.eof ( ) ) {
if (kar >= '0' && kar <= '9') {
counter = kar-48;
while (counter > 1){
uitvoer.put(vorigeKar);
counter--;
}
}else if (kar == '/'){
kar = invoer.get();
uitvoer.put(kar);
}else{
uitvoer.put(kar);
}
vorigeKar = kar;
kar = invoer.get ( );
}
but the problem I have is if need to decode a12bhr, the answer is aaaaaaaaaaaabhr but I can't seem to get the 12 as number without problems, I also can't use any strings or array's.
c++
I believe that you are making following mistake: imagine you give a32, then you read the character a and save it as vorigeKar (previous character, I am , Flemish so I understand Dutch :-) ).
Then you read 3, you understand that it is a number and you repeat vorigeKar three times, which leads to aaa. Then you read 2 and repeat vorigeKar two times, leading to aaaaa (five times, five equals 3 + 2).
You need to learn how to keep on reading numeric characters, and translate them into complete numbers (like 32, or 12 in your case).
Like #Dominique said in his answers, You're doing it wrong.
Let me tell you my logic, you can try it.
Pesudo Code + Logic:
Store word as a char array or string, so that it'll be easy to print at last
Loop{
Read - a //check if it's number by subtracting from '0'
Read - 1 //check if number = true. Store it in int res[] = res*10 + 1
//Also store the previous index in an index array(ie) index of char 'a' if you encounter a number first time.
Read - 2 //check if number = true. Store it in res = res*10 + 2
Read - b , h and so on till "space" character
If you encounter another number, then store it's previous character's index in index array and then store the number in a res[] array.
Now using index array you can get the index of your repeating character to be printed and print it for it's corresponding times which we have stored in the result array.
This goes for the second, third...etc:- numbers in your word till the end of the word
}
First, even though you say you can't use strings, you still need to know the basic principle behind how to turn a stream of digit characters into an integer.
Assuming the number is positive, here is a simple function that turns a series of digits into a number:
#include <iostream>
#include <cctype>
int runningTotal(char ch, int lastNum)
{
return lastNum * 10 + (ch -'0');
}
int main()
{
// As a test
char s[] = "a123b23cd1/";
int totalNumber = 0;
for (size_t i = 0; s[i] != '/'; ++i)
{
char digit = s[i]; // This is the character "read from the file"
if ( isdigit( digit) )
totalNumber = runningTotal(digit, totalNumber);
else
{
if ( totalNumber > 0 )
std::cout << totalNumber << "\n";
totalNumber = 0;
}
}
std::cout << totalNumber;
}
Output:
123
23
1
So what was done? The character array is the "file". I then loop for each character, building up the number. The runningTotal is a function that builds the integer from each digit character encountered. When a non-digit is found, we output that number and start the total from 0 again.
The code does not save the letter to "multiply" -- I leave that to you as homework. But the code above illustrates how to take digits and create the number from them. For using a file, you would simply replace the for loop with the reading of each character from the file.

Regex for removing unneccessary text from inside a specific xml tag

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));
}
}
}
}
}

How to validate mobile number in Regular Expression

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

Comparing Each Character in Java String

I am a beginner at C++, and I am trying to create two strings
any suggestion?
Equals method will not help you in this situation. compare using charAt(). Use two for loop and iterate both strings then add not matching characters to one string and print it at last.
ex:
for(int i=0;i<inputword.length;i++){
for(int j=0;i<inputword2.length;j++){
if(inputword.chatAt(i)==inputword2.charAt(j)){
//here write your logic or remove it from your string
}
}
}
To calculate how many characters at the start one word "overlap" the end of second:
public static int combinedLength(String s1, String s2) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
for (int i = 1; i < s1.length() && i < s2.length(); i++)
if (s1.endsWith(s2.substring(0, i+1)) || s2.endsWith(s1.substring(0, i+1)))
return s1.length() + s2.length() - i;
return s1.length() + s2.length();
}
This works by progressively testing longer letter sequences at the start/end if find if s1 starts with s2's end or visa versa. Because there can be only one such match, the first match found returns the result of the sum of both lengths minus the iteration number. No match returns the sum of both lengths.
Testing:
combinedLength("super", "perfect") ==> 9
combinedLength("perfect", "super") ==> 9
combinedLength("pencil", "eraser") ==> 12

Regex to match certain digit pattern

I want to build a regex expression that allows me to parse through text files with thousands of lines, and each line contains one number with a variable size of digits.
Each number only can contain either the digits 1 or 0 (zero).
The requirement is there MUST be at least 3 1's in the number, and at least one zero. Therefore, the minimum required size of each number is 4 and it has unlimited maximum.
For example, it has to match:
000000111 - has at least 1 zero and 3 ones
1110 - same thing
11111000 - same thing
111 - FAIL, because it's under 4 digits long
0000000011 - FAIL, needs at least 3 ones
Can anyone help me please? My problem is that I can't determine how to find 'at least 3 ones and one zero anywhere in the number', key word being anywhere.
You could match such numbers with:
(?=1*0)(?:0*1){3}[10]*
(?=1*0) make sure there is at least 1 0 with a lookahead (?=...)
(?:0*1){3} match the number with 3 1s
[10]* match the rest or the number
Unless this is strictly a regex exercise/practice, this would be more easily done by hand. (and since the regex would be complicated (im guessing), it would be way more efficient)
int ones = 0;
int zeroes = 0;
for(int i=0;i<str.length();++i)
{
if(str[i] = '0')
++zeroes;
else if(str[i] = '1')
++ones;
}
if(ones+zeroes >= 4 && ones >=3 && zeroes >= 1)
return true;