Subtracting a number from every number in a document - replace

I have a document with hundreds or thousands of numbers. These are page references. The page references have now shifted, so I need to modify every reference.
Using regex in word advanced find and replace, I have tried:
[0-9]{1,}
While this does return each number, it will also then return the next 2 digits of a 3 digit number, which I want to avoid.
The numbers each have 2 or 3 digits and need to subtract 14 from each.
Example:
George V, 116
George Washington Memorial Parkway, 223
Georgian designs, 91, 196, 202; as unique 215
This should become:
George V, 102
George Washington Memorial Parkway, 209
Georgian designs, 77, 182, 188; as unique 203

The following should find all numbers in Word:
<[0-9]{1,}>
< and > represent the start and end of words.

Related

How to convert this line with numbers to a list?

I have this
"208 Wars 209 Xevious 210 Zooming Secrataries 211 Argus 212 DR PLUMBER 213 Goonies 214 KAGE LEGEND 215 Super Chinese 216 TWIN BEE 01 217 Star Soldier 218 BALLOON monster 219 TRACK FIELD 220 80 days"
Its a list of games and as you can see each game has number in front of it starting with 208 up to 220, I would manually have to go trough the list of 500 games and hit ENTER in front of each number to put it into a new line.
Can I somehow do this automatically ?
With any program, is fine with me.Notepad++, maybe Word,...
With Word, to insert ENTER in front of each number with three digits, search <[0-9]{3}> and replace all with ^l^& (line break followed by the found text) with pattern matching using the placeholders option.
Note that this fails if a game name contains a 3-digit number.

Regex for 10 digit phone number with variable spacing

I need to validate that a string follows these rules:
contains numerals
may optionally contain any number of space characters in any position
may not contain any other kind of character
the first two numerals must be one of the set: 02; 03; 07; 08; 13; 18
and the number of numerals must be exactly 10 unless the first two numerals are 1 and 3, in which case the number of numerals may be 10 or 6.
Essentially these are Australian landline (with area code), free-call and 13 numbers.
Ideally the regex should be as implementation-agnostic as possible.
Examples of valid input:
0299998888
02 99998888
02 9999 8888
02 99 998 888
0299 998 888
0299 998888
131999
131 999
13 19 99
1300123456
1300 123456
1300 123 456
1300 12 34 56
1300 12 34 56
PS. I've checked at least 5 other answers and searched for multiple variations of this question, to no avail.
The nearest I have is:
^(?=\d{10}$)(02|03|04|07|08|13|18)\d+
... however this does not account for spacing and won't accept 6 digit numbers beginning with 13.
Note, in theory, the following is acceptable:
1 3 1999
1 3 1 9 9 9
By this I mean that first pair of numerals may have a space between them (as bad as that looks).
Following are examples of random numbers that should fail:
13145 (not enough numerals)
1300-123-456 (hyphens not permitted)
9999 8888 (not enough numerals)
(02) 9999 8888 (parentheses not permitted)
You can make a separate pattern for 13 in alternation:
^(?:(?=(?:\s*\d\s*){10}$)(?:0\s*[2378]|1\s*[38])|(?=(?:\s*\d\s*){6}$)1\s*3).*
Demo: https://regex101.com/r/Hkjus2/2

Regex to match different formats of phone numbers

I have a bunch of numbers which I want to parse.
+79261234567
89261234567
79261234567
9261234567
+7 926 123 45 67
8(926)123-45-67
123-45-67
79261234567
(495)1234567
(495) 123 45 67
89261234567
8-926-123-45-67
8 927 1234 234
8 927 12 12 888
8 927 12 555 12
8 927 123 8 123
What I came with at first is cycle through all the variants like this
(\+[\d]{11}|[\d]{10,11}|\+\d\ [\d]{3}\ [\d]{3}\ [\d]{2}\ [\d]{2}|\d\([\d]{3}\)[\d\-]{9}|[\d\ ]{14,15}|[\d\-]{14,15}|[\d\-]{9}|\(\d\d\d\)[\d\-]{9,10}|\(\d\d\d\)[\d\ ]{9,10}|\(\d\d\d\)[\d\-]{7})
Is there more elegant way to match these numbers?
This regex will match all of the examples and not much extra:
[+]?(\b\d{1,2}[ -]?)?([(]?\d{3}[)]?)((?:[ -]?\d){4,7})(?![ -]?\d)
It can contain between 7 to 12 digits.
Although it would still match with something like this :
+12 (345) 6-7-8 9-0-1
But that should be within acceptable limits.
However, that one could still match part of a longer number.
And to avoid that it would need some negative look-behinds.
(note that there are no look-behinds in javascript regex)
[+]?(?<!\d)(?<!\d[ -])(?:((\d{1,2}[ -]?)?[(]?\d{3}[)]?[ -]?)(\d(?:[ -]?\d){3,6}))(?![ -]?\d)
Here's a regex101 test for that last one.
To have a more elegant solution, you will have to make the pattern more relaxed. One option is to capture 7, 10, or 11 numbers separated by 0 or more delimiters:
\+?(?:[ ()-]*\d){10,11}|(?:[ ()-]*\d){7}
Regex101 Tested

Sort List of Numbers according to Custom Number Sequence

Question :A set of numbers will be passed as input. Also the redefined relationship of the digits 0-9 in ascending order will be passed as input. Based on the redefined relationship, the set of numbers must be listed in ascending order.
Input Format:
The first line will contain the the set of numbers.
The next line will contain the digits 0-9 in the redefined ascending order.
Boundary Conditions:
The size of the set of numbers will be from 2 to 100.
Output Format:
The set of numbers in ascending order as per the redefined order of digits separated by a space.
Example Input/Output 1:
Input:
20 50 11 121
9231476058
Output:
50 11 20 121
Explanation:
121 is a three digit number and hence comes first.
As per the redefined order 2 > 1 > 5.
So 121 is greater than all others and comes in the end.
20 > 11 > 50 and hence in ascending order this is reversed.
Example Input/Output 2:
Input:
319 311 198 420
1948327605
Output:
319 311 420 198
Explanation:
As per the redefined order 1 > 4 > 3
Among 319 and 311, 1 > 9
Hence the final ascending order is 319 311 420 198
My Solution :
if __name__ == '__main__':
list_ = raw_input().split()
num = str(raw_input())
output = sorted(list_, key = num.index)
print(' '.join(output))
I need to know how to do multiple levels of sorting such that it compares indexes of first character, then second character & so on...
This matches your input/output examples, but I had to use descending numbers to get the example answers. Are you sure your explanation is correct? If not, just use 0123456789 instead of 9876543210 in the code below.
The algorithm is to provide a sorting key based on translating the digits of the number into their corresponding rank digits:
import string
def xsort(L,xlat):
def xform(s):
return int(str(s).translate(string.maketrans(xlat,'9876543210')))
return sorted(L,key=xform)
print xsort([20,50,11,121],'9231476058')
print xsort([319,311,198,420],'1948327605')
Output:
[50, 11, 20, 121]
[319, 311, 420, 198]
References: str.translate, string.maketrans

Regular expression for dividing country calling codes

I have a list of calling codes for all countries(the phone number prefixes), I would like to split them up in the
country name and the actual code so I can put then into an xml.
I have tried back and forth but can not get a regexp going that takes all cases into account.
I think it is fairly simple for someone with a bit of experience.
The codes have these formats:
Afghanistan 93
Anguilla 1 264
Antarctica 6721
Antigua and Barbuda 1 268
Bosnia and Herzegovina 387
Canada 1
Congo, Republic of the 242
Cote d'Ivoire 225
Ireland (Eire) 353
United States of America 1
There are around 235 of them in total, but these are the regulars and the exceptions.
^[a-zA-Z]\s,'()] for between 1 and X words and then it is [0-9\s]{1,5}$ for the numbers:
X
XX
XXX
XXXX
X XXX
So if I should express it as a sentence it would be: "from beginning of a line, take all characters (1) including space,'() until you encounter digits, then take all of these including space(2) until you encounter a line break."
I am using TextMate, and the docs says:
TextMate uses the Oniguruma regular
expression library by K. Kosako.
I would appreciate any help given:)
Thank you.
This posix regex should be sufficient: ^[a-zA-Z ]+[0-9 ]+$