首先,我对正则表达式还比较陌生:我已经建立了一个正则表达式供sed使用,对我来说效果很好,它看起来像:
sed 's/^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] [0-9][0-9][0-9][0-9][0-9][0-9].[0-9][0-9][0-9][0-9][0-9][0-9] | info | tst.33.12.carmen | !: //g'
,但我敢肯定所有重复出现的字符都可以简化。我该怎么办?
我要替换:
20180630 180212.407107 | info | tst.33.12.carmen | !:
来自一行文本(前面的时间戳可以是任何数字,第一个'|'之后的字符串是恒定的)
答案 0 :(得分:2)
编辑: :由于OP现在已经提供了输入示例,因此请添加此解决方案。
class ViewController: UIViewController {
let cellNames = ["1", "2", "3"] // ....
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return cellNames.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCellId", for: indexPath) as! CustomCell
let cellName = cellNames[indexPath.item]
cell.nameTextField.text = cellName
return cell
}
}
测试代码是否正常工作
让我们说以下是Input_file:
sed -E 's/^[0-9]{8} [0-9]{6}\.[0-9]{6} \| info \| tst\.[0-9]{2}\.[0-9]{2}\.carmen \| \!:$//' Input_file
现在,在运行上述代码后,将输出以下内容。
cat Input_file
20180630 180212.407107 | info | tst.33.12.carmen | !:
fdfjwhfwifrwvf
vwkdnvkwkvwnvwv
20180630 180212.407107 | info | tst.33.12.carmen | !:
dwbvwbvwvbb
通过sed -E 's/^[0-9]{8} [0-9]{6}\.[0-9]{6} \| info \| tst\.[0-9]{2}\.[0-9]{2}\.carmen \| \!:$//' Input_file
fdfjwhfwifrwvf
vwkdnvkwkvwnvwv
dwbvwbvwvbb
的{{1}}选项,您可以像以下那样使用,但要公平地警告您,它已从解决方案中选择,并且从未进行过测试,因为您的帖子中没有任何样本。
sed
答案 1 :(得分:1)
如果您不希望匹配前缀的确切格式,而只想接受数字,点和空格的某种组合,则可以将第一部分简化为:
[ .0-9]*
完整的sed
表达式如下:
sed 's/^[ .0-9]*| info | tst\.[0-9]*\.[0-9]*\.carmen | !:$//' file