[RESOLVED] Help with regex
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: [RESOLVED] Help with regex

  1. #1
    Join Date
    Oct 2008
    Posts
    30

    [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.

  2. #2
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center