regex to match a word and everything after it?

python, regex

Solution

`re.match` matches only at the beginning of the string. Use `re.search` to match at any position. (See `search()` vs. `match()`)

>>> import re
>>> pat = re.compile(r'(?:/bdata:/b)?\w$')
>>> string = " dnfhndkn data: ndknfdjoj pop"
>>> res = re.search(pat,string)
>>> res
<_sre.SRE_Match object at 0x0000000002838100>
>>> res.group()
'p'

To match everything, you need to change `\w` with `.*`. Also remove `/b`.

>>> import re
>>> pat = re.compile(r'(?:data:).*$')
>>> string = " dnfhndkn data: ndknfdjoj pop"
>>> res = re.search(pat,string)
>>> print res.group()
data: ndknfdjoj pop

Problem

I need to dump some http data as a string from the http packet which i have in string format am trying to use the regular expression below to match 'data:'and everything after it,Its not working . I am new to regex and python ``` >>>import re >>>pat=re.compile(r'(?:/bdata:/b)?\w$') >>>string=" dnfhndkn data: ndknfdjoj pop" >>>res=re.match(pat,string) >>>print res None ```

Original source