过去三天,我一直在尝试在自己的定界符中替换括号。像
( ( [take] function ( [/take] (
外观
[take] function [ [/take]
在[take][/take]
分隔符外部
我尝试过
preg_replace('/[^\[take\]]([)[^\[\take\]]/', '[', $string);
答案 0 :(得分:2)
您需要使用正则表达式:
$string = "( ( [take] function( [/take] (";
$result = preg_replace_callback(
"/(\[take\])(.*?)(\[\/take\])/",
function($m) {return $m[1] . str_replace("(", "[", $m[2]) . $m[3];},
$string
);
echo $result;
preg_replace_callback()
允许您在比赛中执行回调函数。在这种情况下,我们使用匹配组(在括号内)来隔离标签之间的文本,然后在回调中用括号将括号替换。
答案 1 :(得分:0)
如果间距一致,则:
def printParenthesis(str, n):
if(n > 0):
_printParenthesis(str, 0,
n, 0, 0,0);
return;
def _printParenthesis(str, pos, n,
open, close, count):
if(close == n):
for i in str:
print(i, end = "");
print();
return;
else:
if(open > close):
str[pos] = '}';
_printParenthesis(str, pos + 1, n,
open, close + 1, count);
if(open < n):
str[pos] = '{' + chr(65+count);
_printParenthesis(str, pos + 1, n,
open + 1, close, count+1);
# Driver Code
n = 3; //Number of matrices
str = [""] * 2 * n;
printParenthesis(str, n);