Regex With many Overlaps - regex

Let's say I have the string:
"12345"
Is it possible to write a regex that would have the following match results?
["1","2","3","4","5","12","23,"34","45","123","234","345","1234","2345","12345"]
I have seen some similar problems, but not exactly the same.
What i wanted is a regex that can extract any possible number from strings.

The regular expression for this specific output would not be possible, as regular expressions are used to match patterns within a string, not to generate substrings of a given string.
You can do it with python:
input = "12345"
result = [input[i:i+j] for i in range(len(input)) for j in range(1, len(input)-i+1)]
print(sorted(result))
Output: ['1', '12', '123', '1234', '12345', '2', '23', '234', '2345', '3', '34', '345', '4', '45', '5']
Process finished with exit code 0
or JavaScript:
let input = "12345";
let result = [];
for (let i = 0; i < input.length; i++) {
for (let j = 1; j <= input.length - i; j++) {
result.push(input.slice(i, i+j));
}
}
console.log(result.sort());
Output: [
'1', '12', '123',
'1234', '12345', '2',
'23', '234', '2345',
'3', '34', '345',
'4', '45', '5'
]
Or if you want it to look like regex. So funny letters that lead to success, hardly anyone knows how and why, then just like that :D
const c = s => [...Array(1 << s.length)].map((, i) => [...s].filter((, j) => i & (1 << j)).join(''));
console.log(c("12345"));
Output:
[
'', '1', '2', '12',
'3', '13', '23', '123',
'4', '14', '24', '124',
'34', '134', '234', '1234',
'5', '15', '25', '125',
'35', '135', '235', '1235',
'45', '145', '245', '1245',
'345', '1345', '2345', '12345'
]

Related

python 3.5 range trouble

locations:[['120', '40', 'x1'], ['200', '50', 'x2'], ['150', '15', 'x3'], ['240', '60', 'x4'], ['260', '45', 'x5'], ['225', '15', 'x6'], ['273', '20', 'x7'], ['221', '5', 'x8'], ['345', '20', 'x9']]
rangee:[['100', '0', '60', '40', 'x1'], ['80', '10', '30', '5', 'x2'], ['120', '0', '25', '10', 'x3'], ['100', '10', '0', '40', 'x4'], ['70', '0', '10', '25', 'x5'], ['75', '10', '5', '10', 'x6'], ['75', '0', '0', '0', 'x7'], ['0', '0', '0', '0', 'x8'], ['0', '0', '0', '0', 'x9']]
for a in range(int(locations[1][0])+int(rangee[1][0]),int(locations[1][0])-int(rangee[1][1])):
print("x")
I have a locations list with integers.But when I execute this it wont work.Any idea why?
Debug (_ this is not debugging, thats just print out and looking at it_) your data - yours is cryptic to no end:
Use
locations = [['120', '40', 'x1'], ['200', '50', 'x2'], ['150', '15', 'x3'], ['240', '60', 'x4'], ['260', '45', 'x5'], ['225', '15', 'x6'], ['273', '20', 'x7'], ['221', '5', 'x8'], ['345', '20', 'x9']]
rangee = [['100', '0', '60', '40', 'x1'], ['80', '10', '30', '5', 'x2'], ['120', '0', '25', '10', 'x3'], ['100', '10', '0', '40', 'x4'], ['70', '0', '10', '25', 'x5'], ['75', '10', '5', '10', 'x6'], ['75', '0', '0', '0', 'x7'], ['0', '0', '0', '0', 'x8'], ['0', '0', '0', '0', 'x9']]
from_loc = locations[1][0]
from_ran = rangee[1][0]
to_loc = locations[1][0]
to_ran = rangee[1][1]
print ( "from " , from_loc, " + " , from_ran, " to: ", to_loc, " - " , to_ran )
Output:
('from ', '200', ' + ', '80', ' to: ', '200', ' - ', '10')
Test:
print ( range(280,190))
Output:
[]
==> Empty list - for descenting ranges you need to add ,-1 to it for steps:
print ( range(280,190,-1))
Output:
[280, 279, 278, 277, 276, ... , 192, 191]
It depends on your usecase, you might want to do something like:
one = 280
two = 190
print (range( min(one,two), max(one,two))) # will sort them correctly
Output : [190, ..., 279]
OR
print(range(one,two, 1 if one < two else -1))
Which will add the correct stepping to your range depending on its values.

Chart.js: chart does not update

I'm using chart.cs to try a Chart. It works fine so far. Now I want change data dynamicly but the update() functin does not work. What do I wrong?
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js"></script>
</head>
<body>
<button onclick="changedata()">changedata</button>
<canvas id="testChart"></canvas>
<script>
var testChart;
var test_labels = ['06:00', '06:10', '06:20', '06:30', '06:40', '06:50', '07:00'];
var test_data = ['1', '2', '3', '4', '3', '2', '1'];
var ctx = document.getElementById("testChart").getContext('2d');
testChart = new Chart(ctx, {
type: 'line',
data: {
labels: test_labels,
datasets: [{
data: test_data,
borderColor: "#ff0000",
borderWidth: 1,
}]
}
});
function changedata(){
test_data = ['4', '3', '2', '1', '2', '3', '4'];
testChart.update();
}
</script>
</body>
</html>
I think you are missing a little bit in your changedata function, something like
function changedata(){
testChart.data.datasets[0].data = ['4', '3', '2', '1', '2', '3', '4'];
testChart.update();
}

Python: Sort dict of lists by element

I created a dict of lists as such.
{'chans': [['24', 'X', 'RCP', '8400.0', 'mex', '325.0K', '8', '50 1', '17', 266, **datetime.time(22, 0)**, '17', 266, datetime.time(23, 0), '00', '23', '00', 20.604799168660975, '041'],
['24', 'X', 'RCP', '8400.0', 'ody', '325.0K', '4', '40 1', '17', 266, **datetime.time(17, 0)**, '17', 266, datetime.time(18, 0), '00', '18', '00', 16.31949387241184, '053'],
['24', 'X', 'RCP', '8400.0', 'mro', '325.0K', '8', '50 1', '17', 266, **datetime.time(18, 0)**, '17', 266, datetime.time(19, 0), '00', '19', '00', 27.872042154574956, '074']]}
I need to sort this in ascending order by using the date time object (element 10 in each list). I'm stuck at determining the proper key/value. In essence, I'm not sure what to do.
You can use itemgetter, and just select the index of the item in the list not dict, we can't order a dictionnary,
from operator import itemgetter
import datetime
f = {'chans': [['24', 'X', 'RCP', '8400.0', 'mex', '325.0K', '8', '50 1', '17', 266, """datetime.time(22, 0)""", '17', 266, datetime.time(23, 0), '00', '23', '00', 20.604799168660975, '041'],
['24', 'X', 'RCP', '8400.0', 'ody', '325.0K', '4', '40 1', '17', 266, """datetime.time(17, 0)""", '17', 266, datetime.time(18, 0), '00', '18', '00', 16.31949387241184, '053'],
['24', 'X', 'RCP', '8400.0', 'mro', '325.0K', '8', '50 1', '17', 266, """datetime.time(18, 0)""", '17', 266, datetime.time(19, 0), '00', '19', '00', 27.872042154574956, '074']]}
sorted(f['chans'], key=itemgetter(10))
sort function
There is a sort function (in-place) and a sorted function (returns list sorted). Each of these takes an optional argument called key, which is called on each element for comparison. In your case, you want to call sort(dictionary["chans"]), which will sort the lists. Since you want to sort by the 11th element in each list, you would do this:
sort(dictionary["chans"], key = lambda sublist: sublist[10])
The key takes a sublist and returns its 11th element, so sort is sorting the sublists by their 11th elements (the datetime).
You can make key whatever you want to suit your needs.

join digits by removing special characters

I can use some basic regular expression in python to extract digits. But what I am trying to achive is to concat all the digits separated by any chars except space.
>>> ss = ["apple-12.34 ba33na fanc-14.23yapple+45+67.56",
'hello X42 I\'m a Y-32.35 string Z30',
'he33llo 42 I\'m a 32 string -30',
'h3110 23 cat 444.4 rabbit 11 2 dog',
"hello 12 hi 89"]
>>> for s in ss:
... print re.findall("\d+", s)
['12', '34', '33', '14', '23', '45', '67', '56']
['42', '32', '35', '30']
['33', '42', '32', '30']
['3110', '23', '444', '4', '11', '2']
['12', '89']
Expected Results:
['1234', '33', '1423456756']
['42', '3235', '30']
['33', '42', '32', '30']
['3110', '23', '4444', '11', '2']
['12', '89']
give this line a try:
...your for loop:
print re.findall("\d+", re.sub(r'(?<=\d)[^a-zA-Z0-9\s]+(?=\d)','',s))
test with your example, it output:
In [4]: for s in ss:
print re.findall("\d+", re.sub(r'(?<=\d)[^a-zA-Z0-9\s]+(?=\d)','',s))
...:
['1234', '33', '1423', '456756']
['42', '3235', '30']
['33', '42', '32', '30']
['3110', '23', '4444', '11', '2']
['12', '89']
Update since OP changed requirement
The idea is remove those non-empty chars between digits, then extract with \d+
In [4]: for s in ss:
print re.findall("\d+", re.sub(r'(?<=\d)[^\s\d]+(?=\d)','',s))
...:
['1234', '33', '1423456756']
['42', '3235', '30']
['33', '42', '32', '30']
['3110', '23', '4444', '11', '2']
['12', '89']
replace every character other than digit and white space in the string and then split.
>>> import re
>>> line = 'apple-12.34 ba33na fanc-14.23yapple+45+67.56'
>>> list_of_numbers = re.replace('[^\d\s]', '', line).split()
>>> print list_of_numbers
['1234', '33', '1423456756']
Just use
re.findall("\d+", re.sub(r"(?<=\d)[^\s\d]*(?=\d)", "", s))
See this demo
With (?<=\d)[^\s\d]*(?=\d), you will remove any number of characters other than whitespace and digits between two digits. Then, you will be able to extract the remaining digit sequences using a mere \d+ pattern.
Result:
['1234', '33', '1423456756']
['42', '3235', '30']
['33', '42', '32', '30']
['3110', '23', '4444', '11', '2']
['12', '89']

AttributeError: 'int' object has no attribute 'write'

I am reading through a csv file and writing results to a logfile, "p.log". Although I am converting the data into a string before writing it to the file, and doing so successfully through several iterations, I keep running into a situation where I eventually get this error:
AttributeError: 'int' object has no attribute 'write'.
I will provide a snippet of my code as well as the result printed to my screen:
p=open("p.log","w")
for line in f:#parse lines following the headerline
line = line.split(",")
print line, type(str(line))
p.write('string: '+str(line)+'\n')
['14-04-21 08:30:00.973000', 'EEM06211440.5P', 'EEM', '2014-06-21', '40.50', 'P', '16084', 'AUTO', 'PHLX', '0.82', '10', '0.82', '31', '0.88', '31', '0.06', '0.00', '0.06', 'Y', 'AN', '2', 'AN', '2', 'Bid', '31', '0.0000', '2', 'AN\r\n'] <type 'str'>
['14-04-21 08:30:00.974000', 'MU06211425C', 'MU', '2014-06-21', '25.00', 'C', '0', 'AUTO', 'PSE', '1.57', '11', '1.56', '11', '1.67', '30', '0.11', '0.01', '0.10', 'Y', 'Z', '1', 'IXH', '3', 'Mid', '0', '0', '0', '\r\n'] <type 'str'>
['14-04-21 08:30:00.975000', 'QQQ04251481C', 'QQQ', '2014-04-25', '81.00', 'C', '1585', 'AUTO', 'AMEX', '5.70', '1', '5.20', '82', '5.70', '67', '0.50', '0.50', '0.00', 'Y', 'AINX', '4', 'AINX', '4', 'Ask', '67', '-22.3881', '4', 'AINX\r\n'] <type 'str'>
['14-04-21 08:30:00.976000', 'MPEL05171435.66C', 'MPEL', '2014-05-17', '35.66', 'C', '409', 'AUTO', 'ISE', '2.20', '20', '2.20', '384', '3.10', '20', '0.90', '0.00', '0.90', 'N', 'AI', '2', 'I', '1', 'Bid', '384', '94.7917', '2', 'AI\r\n'] <type 'str'>
['14-04-21 08:30:00.978000', 'AAPL7071914500C', 'AAPL', '2014-07-19', '500.00', 'C', '670', 'AUTO', 'AMEX', '34.85', '3', '34.15', '1', '35.05', '12', '0.90', '0.70', '0.20', 'Y', 'A', '1', 'A', '1', 'Mid', '0', '0', '0', '\r\n'] <type 'str'>
Traceback (most recent call last):
File "q1.py", line 83, in <module>
p.write('string: '+str(line)+'\n')
AttributeError: 'int' object has no attribute 'write'
As you can see there are several lines printed without fail, until the line with 'AAPL' in it. Not sure why there's an error on this line and not others.
Somewhere in the code you didn't post is a line that overwrites your variable p with an integer.