extract integer by digit position Swift - swift3

I have several pickerViews throughout the app where users input data. Later, I will display this data as a "summary" page. Displaying the data is clearly easy. However, if the user wants to "edit" the data, I want the same digits to load in the pickerView.
There will be case of 4 digits, 3 digits, 2 digits and a single digit.
So, I need to have the ability to determine which digit is in which position, if there is a digit.
So, if the integer is 321, how to get "1" as the first digit, "2" as the second digit and "3" as the third digit.
Thank you

myPicker.selectRow(row, inComponent: 0, animated: true)
You replace your row and 0 with your values. Then to get the numbers, only thing on my mind now would be something like
while number > 0 {
let digit = number%10 // do what you want with the digit
number = round(number/10)
}

Related

In angular 7 application, how can i modify this mask input to allow a "-" or digit as the first character, and all other characters as digits?

In my angular 7 application I have a method that masks user input. Currently, it will prevent the user from inputting any value other than digits. I need it to allow the user to input either a "-" or a digit as the first character, and all other characters must be digits.
maskInputAmount(e) {
const t = e.target.value.replace(/\D/g, '').match(/(\d{0,3})(\d{0,3})(\d{0,3})/);
e.target.value = t[2] ? t[1] + t[2] + (t[3] ? + t[3] : '') : t[1];
}
I'm coming in midway through this project, there are 3 capture groups in this validator. This isn't necessary, they previously wanted commas every 3 characters but since removed that requirement.
The maximum amount of characters is 7 if the value is positive (9999999) and 8 digits if the value is negative (-9999999).
if you prefix your regex with optional hyphen in this way
/-?(\d{0,3})(\d{0,3})(\d{0,3})/
wont that solve your ask?

Add leading zeros left and right of hyphen

I have a text column in PowerBI, with numeric digits separated by a hyphen. I need the left side to be exactly 5 digits. If it is less, then add leading zeros. The right side needs to be 4 digits. Any less, add leading zeros.
For example:
0002-800 -> 00002-0800
0001-0800 -> 00001-0800
12345-220 -> 12345-0220
Any help is appreciated.
Thanks
Edit the query. Let's assume the text is in a column called "code".
Split the column by delimiter, using the dash as the delimiter
Create a new column that pads the code.1 with 0 if its length is less than 5, else use code.1
Create a new column that pads the code.2 with 0 if its length is less than 4, else use code.2
append the two helper columns with a dash in between
remove the code and helper columns and rename the remaining column to your liking.
Text.Length will return the length of a string, Text.PadStart() will pad text. The formula for step 3 above is
if Text.Length([code.1]) < 5 then
Text.PadStart([code.1], 5, "0")
else
[code.1])
You can do this with one step:
= Table.TransformColumns(
Source, {"Column", each Text.Combine({
Text.PadStart(Text.BeforeDelimiter(_, "-"),5,"0"),
Text.PadStart(Text.AfterDelimiter(_, "-"),4,"0")
},"-"
),type text}
)

Conditional deleting of first digit in SAS variable

I have a character variable called "animid" with values like these:
215298
275899
287796
214896
98154
97856
78-21
213755
21-45
31-457
I want to remove the first digit ("2") only in those numbers that have a length of 6 digits (e.g. 213755, 214896, etc.). I cannot delete the first digit of numbers with a length of 5 or less (e.g. 21-45, 98154).
I used the following code trying to subtract the last 5 digits
data new;
set old;
new_animid =substr (animid,max(1,length(animid)-4),5);
run;
This code effectively keep the last 5 digits for each value. However, it also converts numbers like 31-457 to 1-457 (which is a result that I don't want. I only want to delete the number first digit ONLY if the value has 6 digits AND it starts with "2").
I basically ask if there is a way to state conditions to the "substr" statement (or other method in SAS). Something that will allow me to delete the number "2" but ONLY in those numbers that effectively start with the digit "2" AND that have 6 digits.
To remove the first digit just use SUBSTR(,2).
new_animid = animid ;
if animid =: '2' and length(animid)=6 then new_animid = substr(animid,2);
Use regular expression:
_animid=prxchange('s/^2(\d{5})/$1/',-1,animid);

Regular Expression - Two Digit Range (23-79)?

I have been reading the regex questions on this site but my issue seems to be a bit different. I need to match a 2 digit number, such as 23 through 75. I am doing this on an HP-UX Unix system. I found examples of 3 - 44 but or any digit number, nothing that is fixed in length, which is a bit surprising, but perhaps I am not understand the variable length example answer.
Since you're not indicating whether this is in addition to any other characters (or in the middle of a larger string), I've included the logic here to indicate what you would need to match the number portion of a string. This should get you there. We're creating a range for the second numbers we're looking for only allowing those characters. Then we're comparing it to the other ranges as an or:
(2[3456789]|[3456][0-9]|7[012345])
As oded noted you can do this as well since sub ranges are also accepted (depends on the implementation of REGEX in the application you're using):
(2[3-9]|[3-6][0-9]|7[0-5])
Based on the title you would change the last 5 to a 9 to go from 75-79:
(2[3-9]|[3-6][0-9]|7[0-9])
If you are trying to match these numbers specifically as a string (from start to end) then you would use the modifiers ^ and $ to indicate the beginning and end of the string.
There is an excellent technical reference of Regex ranges here:
http://www.regular-expressions.info/numericranges.html
If you're using something like grep and trying to match lines that contain the number with other content then you might do something like this for ranges thru 79:
grep "[^0-9]?(2[3-9]|[3-6][0-9]|7[0-9])[^0-9]?" folder
This tool is exactly what you need: Regex_For_Range
From 29 to 79: \b(2[3-9]|[3-7][0-9])\b
From 29 to 75: \b(29|[3-6][0-9]|7[0-5])\b
And just for fun, from 192 to 1742: \b(19[2-9]|[2-9][0-9]{2}|1[0-6][0-9]{2}|17[0-3][0-9]|174[0-2])\b :)
If I want 2 digit range 0-63
/^[0-9]|[0-5][0-9]|6[0-3]$/
[0-9] will allow single digit from 0 to 9
[0-5][0-9] will allow from 00 to 59
6[0-3] will allow from 60 till 63
This way you can take Regular Expression for any Two Digit Range
You have two classes of numbers you want to match:
the digit 2, followed by one of the digits between 3 and 9
one of the digits between 3 and 7, followed by any digit
Edit: Well, that's the title's range (23-79). Within your question (23-75), you have three:
the digit 2, followed by one of the digits between 3 and 9
one of the digits between 3 and 6, followed by any digit
the digit 7, followed by one of the digits between 0 and 5
Just to add to this, here is a solution for generating the string from the accepted answer in javascript. You can click "Run Code Snippet" to enter your own bounds and get your own string.
function regexRangeString(lower,upper){
let current=lower;
let nextRange=function(){
let currentString=String(current);
let len=currentString.length;
let string="";
let newUpper;
for(let digit=0;digit<len;digit++){
let index=len-digit-1;
let lower=Number(currentString[index]);
let thisString="";
for(let u=9;u>=lower;u--){
let us=currentString.substring(0,index)+u+currentString.substring(index+1,len);
if(Number(us)<=upper){
if(lower==u){
thisString=lower;
}
else{
thisString=`[${lower}-${u}]`;
}
currentString=currentString.substring(0,index)+u+currentString.substring(index+1,len);
break;
}
}
if(thisString!="[0-9]"){
string=currentString.substring(0,index)+thisString+string;
break;
}
else{
string=thisString+string
}
}
current=Number(currentString)+1;
return string
}
let string=""
while(current<upper){
string+="|"+nextRange(current);
}
string="("+string.slice(1)+")";
return string
}
let lower=prompt("Enter Lower Bound")
let upper=prompt("Enter Upper Bound")
alert(regexRangeString(lower,upper))
For example:
regexRangeString(72,189)
generates the following output string:
(7[2-9]|[8-9][0-9]|1[0-8][0-9])
This should do it:
/^([2][3-9]|[3-6][0-9]|[7][0-5])$/
^ and $ will make it strict that it will match only 2 numbers, so in case that you have i.e 234 it won't work.

Validation for a 10 digit phone number

I'm looking for a simple regex that will validate a 10 digit phone number. I'd like to make sure that the number is exactly 10 digits, no letters, hyphens or parens and that the first two digits do not start with 0 or 1. Can someone help out?
/[2-9]{2}\d{8}/
^[2-9]{2}[0-9]{8}$
I consider [0-9] to be better to read than \d, especially considering the preceding [2-9]
The ^ and $ ensure that the input string consists ONLY of those 8 characters - otherwise it is not guaranteed that the input string is not larger - i.e. "12345678901" would match the regex w/o those two characters - although it is 11 chars and starts with a 1!
As Randal pointed out, this question is not consistent with the way phone numbers are formatted in North America (even though the OP stated 'first two digits do not start with 0 or 1'). A better regex for North American phone numbers would be:
^[2-9]{1}[0-9]{9}$
For example, Washington DC's area code is (202). NYC has area code (212). Northern New Jersey has (201).
But more accurately, the NANP has a lot of rules as it relates to what is allowed in area code and exchange (first six digits). This regex should still cover most cases. https://en.wikipedia.org/wiki/North_American_Numbering_Plan
This regex script might help out. I essentially strips any "punctuation" characters, including a leading 1-, then validates it is 10 digits.
The extra part you probably don't need is the formatting to 000-000-0000
formatPhone = function() {
var phone = this.value;
phone = phone.replace(/^1(|-|\(|\)|\.| )*|-|\(|\)|\.| /g, '');
if(phone.length === 10) {
this.value = phone.slice(0,3) + '-' + phone.slice(3,6) + '-' + phone.slice(6,10);
}
}
The Phone Numbers will be of 10 digits, and it will start from 7,8 and 9
[RegularExpression("^([07][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] | 8[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] | 9[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])$", ErrorMessage = "Enter Valid Mobile Number")]
reference : http://www.regular-expressions.info/numericranges.html