返回python中括号内的值

时间:2016-11-05 12:53:30

标签: python regex parentheses

我正在尝试在括号内提取以下行的值。

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" ></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js" ></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular-aria.min.js" >

预期答案是:

    line=(panel, wxID_EXIT, wxT("Quit"),wxPoint(20, 20))

但使用以下

    panel, wxID_EXIT, wxT("Quit"),wxPoint(20, 20)

我只收到以下输出。

    pattern=re.compile(r'\((.*?)\)')
    match = re.search(pattern, line)

不幸的是,我对正则表达式的了解非常有限。有人可以帮助我得到预期的答案。

3 个答案:

答案 0 :(得分:0)

使用.*?,您关闭了正则表达式的贪婪方面。只需删除问号再次启用它(这是默认设置):

pattern=re.compile(r'\((.*)\)')

完整测试:

import re
line='(panel, wxID_EXIT, wxT("Quit"),wxPoint(20, 20))'
pattern=re.compile(r'\((.*)\)')
match = re.search(pattern, line)
print(match.group(1)=='panel, wxID_EXIT, wxT("Quit"),wxPoint(20, 20)')

True

答案 1 :(得分:0)

我认为你可以在这里避免使用RegEx:

text = '''line=(panel, wxID_EXIT, wxT("Quit"),wxPoint(20, 20))'''
parts = text.split("=")
result = parts[1][1:-1]

答案 2 :(得分:0)

你可以这样做:

horizontalBarChartView.noDataText = "You need to provide data for the chart."
    horizontalBarChartView.isUserInteractionEnabled = false
    horizontalBarChartView.drawGridBackgroundEnabled = false
    horizontalBarChartView.drawValueAboveBarEnabled = false
    horizontalBarChartView.legend.enabled = false
    horizontalBarChartView.drawBarShadowEnabled = false
    horizontalBarChartView.chartDescription?.text = ""
    horizontalBarChartView.setExtraOffsets(left: 0, top: 0, right: 0, bottom: 0)

    // Right y axis
    let barRightAxis = horizontalBarChartView.leftAxis
    barRightAxis.drawGridLinesEnabled = false
    barRightAxis.enabled = false

    let formato:BarChartFormatter = BarChartFormatter()
    let xaxis:XAxis = XAxis()

    // Left y axis
    let barLeftAxis = horizontalBarChartView.rightAxis
    let count = 5
    barLeftAxis.setLabelCount(count, force: true)
    barLeftAxis.drawGridLinesEnabled = false
    barLeftAxis.axisLineWidth = 2
    barLeftAxis.axisLineColor = UIColor.white
    barLeftAxis.labelTextColor = UIColor.white
    barLeftAxis.axisMinimum = 0.0
    barLeftAxis.axisMaximum = 4.0
    barLeftAxis.granularityEnabled = true
    barLeftAxis.granularity = 1.0
    barLeftAxis.decimals = 0

    // x axis
    let barXaXis = horizontalBarChartView.xAxis
    barXaXis.drawGridLinesEnabled = false
    barXaXis.axisLineColor = UIColor.white
    barXaXis.axisLineWidth = 2
    barXaXis.labelTextColor = UIColor.white
    barXaXis.labelPosition = .bottomInside
    barXaXis.granularityEnabled = true
    barXaXis.granularity = 1.0
    barXaXis.decimals = 0

    var dataEntries: [BarChartDataEntry] = []
    var colors: [UIColor] = []

    let theScoreVec = [1.0, 2.0, 3.0, 4.0, 3.0, 0.0, 2.0, 0.0, 1.0, 4.0]
    for i in 0..<10 {
        let theScore = theScoreVec[i]
        if (theScore == 0.0){
            colors.append(UIColor.clear)
            let dataEntry = BarChartDataEntry(x: Double(i), y : Double(4.0))
            dataEntries.append(dataEntry)
        } else {
            colors.append(UIColor.red)
            let dataEntry = BarChartDataEntry(x: Double(i), y: Double(theScore))
            dataEntries.append(dataEntry)
        }
    }
    xaxis.valueFormatter = formato
    barXaXis.valueFormatter = xaxis.valueFormatter

    for i in 0..<10{
        formato.stringForValue(Double(i), axis: xaxis)

    }
    xaxis.valueFormatter = formato
    barXaXis.valueFormatter = xaxis.valueFormatter

    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Completion")
    chartDataSet.colors = colors
    let chartData = BarChartData(dataSet: chartDataSet)
    chartData.setDrawValues(false)
    horizontalBarChartView.data = chartData

或者,在直接Python中,你可以这样做:

>>> line='(panel, wxID_EXIT, wxT("Quit"),wxPoint(20, 20))'
>>> re.search(r'^\s*\((.*)\)\s*$', line).group(1)
'panel, wxID_EXIT, wxT("Quit"),wxPoint(20, 20)'

首先使用>>> line.strip()[1:-1] 'panel, wxID_EXIT, wxT("Quit"),wxPoint(20, 20)' 剥离空格,然后使用.strip()

修剪字符串两侧的一个字符