例如,我可以在PHP中使用Python脚本:
exec("python script.py params",$result);
Run Code Online (Sandbox Code Playgroud)
其中"script.py" - 脚本名称和变量$result保存输出数据.
我如何用Ruby制作它?我的意思是,从Ruby调用Python脚本.
我想扩展类"list"的功能并为事件添加自定义处理程序:"将新项目添加到列表"和"从列表中删除项目".对于这个任务,我不想使用组合,更好的是继承.
我尝试做了什么:
class ExtendedList(list):
def append(self, obj):
super(ExtendedList, self).append(obj)
print('Added new item')
def extend(self, collection):
if (hasattr(collection, '__iter__') or hasattr(collection, '__getitem__')) and len(collection)>0:
for item in collection:
self.append(item)
def insert(self, index, obj):
super(ExtendedList, self).insert(index, obj)
print('Added new item')
def remove(self, value):
super(ExtendedList, self).remove(value)
print('Item removed')
Run Code Online (Sandbox Code Playgroud)
但它无法正常工作.我无法捕捉所有添加和删除事件.例如:
collection = ExtendedList()
collection.append('First item')
# Out: "Added new item\n"; collection now is: ['First item']
collection.extend(['Second item', 'Third item'])
# Out: "Added new item\nAdded new item\n"; collection now is: ['First item', 'Second item', 'Third …Run Code Online (Sandbox Code Playgroud) 我如何通过PHP正则表达式找到并替换字符串中的所有非包装项?
例如,我有源字符串"2a {2} b2ac1 {1} a {2} aab12 {1} b2a {1} 2"并尝试查找符号"2",这些符号未被"{"和"}"覆盖,在此之后用"{3}"替换它:
$input_lines = "2a{2}b2ac1{1}a{2}aab12{1}b2a{1}2";
$regex = "/[^\{](2)[^\}]/";
$input_lines = preg_replace("/[^\{](2)[^\}]/", "{3}", $input_lines);
echo $input_lines;
// 2a{2}{3}c1{1}a{2}aab{3}1}{3}{1{3}
Run Code Online (Sandbox Code Playgroud)
你怎么看,它现在工作:(