正则表达式在Swift中查找和替换字符串开头和结尾文本

时间:2016-03-18 21:42:35

标签: ios swift swift2

我怎样才能在Swift 2.x中以(例如)“AB”开头并以“ZX”结尾来查找和替换字符串?

编辑:我想保留AB和ZX字符串。 e.g。

Notes
-----
See splev for evaluation of the spline and its derivatives. Uses the
FORTRAN routine curfit from FITPACK.
If provided, knots `t` must satisfy the Schoenberg-Whitney conditions,
i.e., there must be a subset of data points ``x[j]`` such that
``t[j] < x[j] < t[j+k+1]``, for ``j=0, 1,...,n-k-2``.
References
----------
Based on algorithms described in [1]_, [2]_, [3]_, and [4]_:
.. [1] P. Dierckx, "An algorithm for smoothing, differentiation and
   integration of experimental data using spline functions",
   J.Comp.Appl.Maths 1 (1975) 165-184.
.. [2] P. Dierckx, "A fast algorithm for smoothing data on a rectangular
   grid while using spline functions", SIAM J.Numer.Anal. 19 (1982)
   1286-1304.
.. [3] P. Dierckx, "An improved algorithm for curve fitting with spline
   functions", report tw54, Dept. Computer Science,K.U. Leuven, 1981.
.. [4] P. Dierckx, "Curve and surface fitting with splines", Monographs on
   Numerical Analysis, Oxford University Press, 1993.

(我想用空字符串(“”)替换,但保持在AB和ZX之间)

2 个答案:

答案 0 :(得分:0)

您可以按照以下方式执行此操作:"\\bAB\\w+ZX\\b"

var strings = ["AB_any_ZX","AB_whatever_ZX","HELLO"]

for (index, str) in strings.enumerate() {
    if let range = str.rangeOfString("\\bAB\\w+ZX\\b", options: .RegularExpressionSearch) {
        strings[index] = str.substringWithRange(range.startIndex.successor().successor()..<range.endIndex.predecessor().predecessor())

    }
}

print(strings)  // "["_any_", "_whatever_", "HELLO"]\n"

答案 1 :(得分:0)

这是否适合您的需要?

EL=$(tr '\n' ',' <<< "$DEL" | sed 's/,$//')
相关问题