Python - 如何使用正则表达式查找文本?

时间:2016-05-10 23:53:14

标签: python regex

请耐心等待我,我是Python新手。我有一个文本,我希望在^s之后获取值,直到下一个^,例如^s100^,然后值为100。这是我到目前为止所尝试的:

#!/usr/bin/python

import re

text="^request^ #13#10#13#10^s100^GET http://facebook.com #13#10Host: http://facebook.com #13#10X-Online-Host: http://facebook.com #13#10X-Forward-Host: http://facebook.com #13#10Connection: Keep-Alive#13#10#13#10"
if re.split(r'\^s',text):
    print "found it"

问题是即使我将正则表达式更改为found it并且基本上任何文本都会返回re.split(r'\^bla',text),它总会返回found it请帮我修复它。

2 个答案:

答案 0 :(得分:1)

您可能想要的是re.search

const {div, input, label, h2, makeDOMDriver} = CycleDOM;
const isolate = CycleIsolate;


function intent(DOM){

  return DOM.select('.slider').events('input')
    .map(ev => ev.target.value);
 };

function model(action$, props$){

   const intialValue$ = props$.map(props => props.init).first();
   const value$ = intialValue$.concat(action$)

   const state$ = Rx.Observable.combineLatest(value$, props$, 
    (value,props) => {
      return {
        label: props.label,
        unit: props.unit,
        min: props.min,
        max: props.max,
        value: value
      }
    }

   );

   return state$
};

function view(state$){
 return  state$.map( state =>
      div('.labled-slider',
          [
            label('.label', `${state.label}: ${state.value} ${state.unit}`),
            input('.slider', {type: 'range', min: state.min, max: state.max, valeu : state.value})
          ]
      )
    )
};

function LabeledSlider(sources) {
  const change$ = intent(sources.DOM);
  const state$  = model(change$, sources.props)
  const vtree$  = view(state$)
  return {
    DOM: vtree$
  };
};


const IsolatedLabelSlider = function (sources){
  return isolate(LabeledSlider)(sources)
}

function main(sources){

let labelSliderArray =  Rx.Observable.from(
[
   {
    label: 'Height',
    unit: 'in',
    min: 40,
    max: 84,
    init: 50
  },
  {
    label: 'Weight',
    unit: 'ibs',
    min: 40,
    max: 84,
    init: 50
  },

   {
    label: 'Age',
    unit: 'years',
    min: 10,
    max:  65,
    init: 20
  }

]);
let labelSinks = labelSliderArray.map(sProps => IsolatedLabelSlider({DOM: sources.DOM,  props:  Rx.Observable.of(sProps)}) )
let labelVtree$ = labelSinks.map(l => l.DOM)

 return  {
   DOM: labelVtree$
 };
}

const drivers = {
  DOM: makeDOMDriver('#app')
}

Cycle.run(main, drivers);

答案 1 :(得分:0)

您不需要尽可能多的代码,请尝试以下操作:

import re
text = "^request^ #13#10#13#10^s100^GET http://facebook.com #13#10Host: http://facebook.com #13#10X-Online-Host: http://facebook.com #13#10X-Forward-Host: http://facebook.com #13#10Connection: Keep-Alive#13#10#13#10"
match = re.search(r"\^s(\d+)\^", text)
if match:
    print match.group(1)

正则表达式说明:

\^s(\d+)\^

Match the character “^” literally «\^»
Match the character “s” literally (case sensitive) «s»
Match the regex below and capture its match into backreference number 1 «(\d+)»
   Match a single character that is a “digit” (ASCII 0–9 only) «\d+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “^” literally «\^»

Ideone Demo