删除所有引号和引号之间的特殊字符。
我有这行:"The quick "brown-fox" "jumps" over the lazy dog"
,我想做的是将引号放在两端,但是要删除它们之间的所有引号和特殊字符,因此例如,该行将变为:{{1 }}。
我有一点RegEx可以找到带引号的字符串:"The quick brown fox jumps over the lazy dog"
但是;我想做的实际上是删除引号之间的特殊字符。我只是最近才发现正则表达式,它可以完成很多工作,但我仍在学习。
答案 0 :(得分:1)
您可以尝试:
import keras
import numpy as np
def generate_data(a, nt):
ny = len(a)
max_lag = -1
for iy in range(ny):
for jy in range(ny):
max_lag = max(max_lag, len(a[iy][jy]))
d2 = np.zeros(shape=(nt, ny, max_lag), dtype=float)
# seed random number generator
np.random.seed(1)
d2[0] = np.random.rand(ny, max_lag) # initalize lagged values for time 0
d2[:, :, 0] = np.nan # Set all the y values to nan
for it in range(nt):
for iy in range(ny):
da = 0
for jy in range(ny):
lag_coeff = a[iy][jy][1:]
n_terms = len(lag_coeff)
if n_terms > 0:
lags = d2[it][jy][1:(n_terms + 1)]
this_term = np.matmul(lags, lag_coeff)
da = da - this_term
d2[it][iy][0] = da
if it < (nt - 1):
d2[it + 1][iy][1:max_lag] = d2[it][iy][:(max_lag - 1)]
x2 = d2[:, :, 1:]
x2_1 = x2.shape[1]
x2_2 = x2.shape[2]
x2_product = x2_1 * x2_2
x2 = x2.reshape((nt, x2_product))
y2 = d2[:, :, 0].reshape((nt, ny))
return max_lag, ny, x2, y2
def data_split(fraction_train, fraction_validate, x2, y2):
nt = y2.shape[0]
n_train = round(fraction_train * nt)
n_validation = round(fraction_validate * n_train)
x_train_val = x2[:(n_train + n_validation)]
y_train_val = y2[:(n_train + n_validation)]
x_train = x_train_val[:n_train]
x_validation = x_train_val[n_train:]
y_train = y_train_val[:n_train]
y_validation = y_train_val[n_train:]
x_test = x2[(n_train + n_validation):]
y_test = y2[(n_train + n_validation):]
return x_train, y_train, x_test, y_test, x_validation, y_validation
nt = 1000
a = [[[1, -1.1, 0.7, .169], [0, -.5, .1]],
[[0, -.04, .104], [1, -1.2, .6, .1, -.531]]]
max_lag, ny, x_data, y_data = generate_data(a, nt)
fraction_train = 0.8
fraction_validate = 0.1
x_train, y_train, x_test, y_test, x_validation, y_validation = \
data_split(fraction_train, fraction_validate, x_data, y_data)
x_input = keras.Input(shape=(1, None, x_train.shape[1]), name='x_input')
print('x_input.shape: ', x_input.shape)
ny_output = keras.layers.Dense(ny, name='dense_a', use_bias=False)(x_input)
print('ny_output.shape:', ny_output.shape)
print('max_lag: ', max_lag)
output_list = list()
ky_start = 0
stored_lags = max_lag - 1
for iy in range(ny):
ky_end = ky_start + stored_lags - 1
print('append output, {}:{}'.format(iy, iy + 1))
output_list.append(ny_output[:, :, :, iy:(iy + 1)]) # Add the next time step
print('append input, {}:{}'.format(ky_start, ky_end))
output_list.append(x_input[:, :, :, ky_start:ky_end])
ky_start = ky_end + 1 # Move to the next set of lags
outputs = keras.layers.Concatenate(axis=3)(output_list)
model = keras.Model(inputs=x_input, outputs=outputs)
print(model.summary())
JS演示:https://jsfiddle.net/jorgesanure/vca2gsxm/
要从崇高的角度使用它,请仅复制此let string = `"The quick "brown-fox" "jumps" over the lazy dog"`.replace(/(?<=.)"(?=.)/g, "");
console.log(string);
答案 1 :(得分:0)
可以肯定的是,有很多有效的方法/表达式可以做到这一点,但我想不到一个,也许我们会尝试三种表达式,其中一种类似于:
((?:[^"\r\n-])*)|((?!(^"|"$)).)+?
并替换为$1
,后跟一个空格,它将打印:
"The quick brown fox jumps over the lazy dog "
然后用第二个表达式
\s{2,}
并替换为一个空格:
"The quick brown fox jumps over the lazy dog "
并带有第三个表达式,
\s*"\s*
替换"
,我们可能会得到:
"The quick brown fox jumps over the lazy dog"
不需要的字符进入字符类:
[^"\r\nUndesired_Chars_Goes_here-]