-
October 6th, 2009, 06:13 AM
#1
[RESOLVED] Help with regex
I have one string:
Code:
asdassda_folder_asdswdfd_file_asedswfds
When i try:
Code:
test = re.findall("_(.+)_", "asdassda_folder_asdswdfd_file_asedswfds")
The output is:
folder_asdswdfd_file
And i need to capture _folder_ and _file_ , the other text that surround can change.
Tryed several ways but couldnĀ“t success, perhaps someone can point me to the right direction.
Thanks in advance.
-
April 20th, 2010, 03:57 AM
#2
Re: Help with regex
Code:
test = re.findall("_(.+)_", "asdassda_folder_asdswdfd_file_asedswfds")
The + operator is greedy and will take as much as possible that fits the pattern. Adding the "?" at the end means it will take as little as possible.
The result is: ['folder', 'file']
If the result you want is: ['_folder_', '_file_']
then you need:
Code:
test = re.findall("(_.+_)", "asdassda_folder_asdswdfd_file_asedswfds")
Elrond
A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
-- George Steiner
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|