?
Solved

regex for compliment

Posted on 2008-11-13
8
Medium Priority
?
365 Views
Last Modified: 2012-05-05
Hi,

      I have a string "Matches any character except a newline". Is it possible to match every other part of this string except "a newline". In other words I need to cut "a newline" and take only the first part of it. How is it possible

BRgds,

kNish
0
Comment
Question by:eyeqube
8 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 22957560
(.*)a newline
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 22957618
eyeqube,

If you dot character is matching the newline character and you using this in Python make sure you don't have the DOTALL flag in your expression.  The default is the character won't match a newline but that flag can change it.  If I have misunderstood what you asked then let me know.

Let me know if you have any questions or need more information.

b0lsc0tt
0
 

Author Comment

by:eyeqube
ID: 22957806
Hi,

     I tried ozo's suggestion. It does not give me the desired result. let me explain my question again in other words.

   I have a string that has extra information. "//abcd-999/beeHive/hexagon/honey/sugar/honey". This string has the last two folder names "/sugar/honey". I need to have only "//abcd-999/beeHive/hexagon/honey" part stored.

BRgds,

kNish
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:ddrudik
ID: 22957935
It seems the string in the original question was misinterpreted, it appears that was just an example string and not meant to be interepreted as a . matches \n question.

It depends on your rules for matching "/sugar/honey" but if it is literal:
re.sub("/sugar/honey$", "", subject)

Open in new window

0
 
LVL 29

Expert Comment

by:pepr
ID: 22959915
It depends on what you really want. But such cases can usually be better solved without regular expressions. It depends what exactly should be removed/replaced. You should clarify your intention better.

Try the snippet below to see how the string can be split by '/', the resulting list modified or extracted and then the other list joined again into a string.
s1 = '//abcd-999/beeHive/hexagon/honey/sugar/honey'
print repr(s1)
 
lst = s1.split('/')
print lst
 
s2 = '/'.join(lst)
print repr(s2)
 
# One possible manipulation.
lst2 = lst[0:5] + lst[-1:]
print lst2
 
s3 = '/'.join(lst2)
print repr(s3)

Open in new window

0
 
LVL 29

Expert Comment

by:pepr
ID: 22959921
The above prints the output...

'//abcd-999/beeHive/hexagon/honey/sugar/honey'
['', '', 'abcd-999', 'beeHive', 'hexagon', 'honey', 'sugar', 'honey']
'//abcd-999/beeHive/hexagon/honey/sugar/honey'
['', '', 'abcd-999', 'beeHive', 'hexagon', 'honey']
'//abcd-999/beeHive/hexagon/honey'
0
 
LVL 29

Expert Comment

by:pepr
ID: 22967750
Well, it was a little joke ;) The lst2 assignment command was to be...

lst2 = lst[:-2]   # i.e. copy the lst form the beginning except the last two elements
0
 
LVL 7

Accepted Solution

by:
jdpipe earned 1500 total points
ID: 23001676
is this what you're looking for?




>>> s = "hello\nthis is the rest"
>>> print "'%s'" % s.split("\n",1)[0]
'hello'

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question