-1

Possible Duplicate:
Python split string on regex

How do I split some text using Python's re module into two parts: the text before a special word cut and the rest of the text following it.

2
  • 4
    Welcome to SO! Please improve the quality of your question. Commented Sep 21, 2011 at 12:49
  • A great way to improve the quality of your question is to search for all the questions which are exactly like this one. Here's a list: stackoverflow.com/search?q=python+re+split. Commented Sep 21, 2011 at 13:45

2 Answers 2

5

You can do it with re:

>>> import re
>>> re.split('cut', s, 1) # Split only once.

But in this case you can just use str.split:

>>> s.split('cut', 1) # Split only once.
Sign up to request clarification or add additional context in comments.

Comments

1

Check this, might help you

>>> re.compile('[0-9]+').split("hel2l3o")
['hel', 'l', 'o']
>>> 

>>> re.compile('cut').split("hellocutworldcutpython")
['hello', 'world', 'python']

split about first cut 

>>> l=re.compile('cut').split("hellocutworldcutpython")
>>> print l[0], string.join([l[i] for i in range(1, len(l))], "")
hello worldpython

4 Comments

need **TWO part ['hello', 'worldcutpython']
can you please explain it clearly
Need text only before txt and after.
>>> l=re.compile('cut').split("hellocutworldcutpython") >>> print l[0], string.join([l[i] for i in range(1, len(l))], "") hello worldpython

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.