我的这个列表包含部分语音标签及其细节:['VNW(pers,pron,nomin,red,2v,ev)', 'WW(pv,tgw,met-t)', 'LID(bep,stan,rest)']
。如您所见,角色之间没有空格,因此可以将其视为一个单词。
现在我需要一个只包含部分语音标签的新列表,例如['VNW', 'WW', 'LID']
。
我尝试使用像pattern = re.compile(r'(.*)')
这样的正则表达式删除括号及其中的所有内容
我也试图只匹配大写字母,但我无法做到正确。建议?
答案 0 :(得分:3)
这种情况不需要正则表达式。按 import UIKit
import AVFoundation
class CameraViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
@IBOutlet weak var messageView: UIView!
@IBOutlet weak var messageLbl: UILabel!
@IBOutlet weak var topBar: UIView!
var captureSession: AVCaptureSession?
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
var qrCodeFrameView: UIView?
override func viewDidLoad() {
super.viewDidLoad()
let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
do {
let input = try AVCaptureDeviceInput(device: captureDevice) as AVCaptureDeviceInput
captureSession = AVCaptureSession()
captureSession?.addInput(input)
} catch {
print(error)
return
}
let captureMetaDataOutput = AVCaptureMetadataOutput()
captureSession?.addOutput(captureMetaDataOutput)
captureMetaDataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
captureMetaDataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode]
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
videoPreviewLayer?.frame = view.layer.bounds
view.layer.addSublayer(videoPreviewLayer!)
captureSession?.startRunning()
view.bringSubview(toFront: messageLbl)
view.bringSubview(toFront: topBar)
view.bringSubview(toFront: messageView)
qrCodeFrameView = UIView()
if let newFrame = qrCodeFrameView {
newFrame.layer.borderColor = UIColor.green.cgColor
newFrame.layer.borderWidth = 2
view.addSubview(newFrame)
view.bringSubview(toFront: newFrame)
}
}
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
if metadataObjects == nil || metadataObjects.count == 0 {
qrCodeFrameView?.frame = CGRect.zero
messageLbl.text = "No QR code is detected"
return
}
let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
if metadataObj.type == AVMetadataObjectTypeQRCode {
let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
qrCodeFrameView?.frame = barCodeObject!.bounds
if metadataObj.stringValue != nil {
messageLbl.text = metadataObj.stringValue
}
}
}
@IBAction func goToList(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
}
拆分;然后只获得第一部分。
class MovieListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var movieTableView: UITableView!
var movie:[Movie] = [Movie]()
var currentMovies:[Movie] = [Movie]()
var counter = 0
override func viewDidLoad() {
super.viewDidLoad()
movie = Helpers.downloadJson()
self.movieTableView.reloadData()
sort()
}
func sort() {
movie.sort(by: { $0.releaseYear < $1.releaseYear })
movieTableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return movie.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MovieCell") as! MovieCell
cell.createCell(movie: movie[indexPath.row])
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedMovie = movie[indexPath.row]
performSegue(withIdentifier: "goToDetail", sender: selectedMovie)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let nextVC = segue.destination as! DetailViewController
nextVC.movie = sender as! Movie
}
@IBAction func addTapped(_ sender: Any) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let nextView = storyBoard.instantiateViewController(withIdentifier: "goToCamera") as! CameraViewController
self.present(nextView, animated: true, completion: nil)
}
}
(
答案 1 :(得分:1)
一些可能的解决方案是:
使用循环删除括号
l = ['VNW(pers,pron,nomin,red,2v,ev)', 'WW(pv,tgw,met-t)', 'LID(bep,stan,rest)']
for i in range(len(l)):
i1,i2=l[i].find('('),l[i].find(')')
l[i]=l[i][:i1]+l[i][i2+1:]
print l
使用正则表达式
import re
pattern = r'\([^)]*\)'
l = ['VNW(pers,pron,nomin,red,2v,ev)', 'WW(pv,tgw,met-t)', 'LID(bep,stan,rest)']
for i in range(len(l)):
l[i] = re.sub(pattern, '', l[i])
print l
输出: [&#39; VNW&#39;,&#39; WW&#39;,&#39; LID&#39;]
答案 2 :(得分:0)
使用str.find()
函数的简短解决方案:
l = ['VNW(pers,pron,nomin,red,2v,ev)', 'WW(pv,tgw,met-t)', 'LID(bep,stan,rest)']
result = [i[:i.find('(')] for i in l]
result
内容:
['VNW', 'WW', 'LID']
答案 3 :(得分:0)
例如:
In [102]: s=['VNW(pers,pron,nomin,red,2v,ev)', 'WW(pv,tgw,met-t)', 'LID(bep,stan,rest)']
In [103]: [x.split('(', 1)[0] for x in s]
Out[103]: ['VNW', 'WW', 'LID']