Search code examples
regexstringstring-parsing

Regular Expression Troubles


Given the following type of string:

"#First Thing# #Another One##No Space# Main String #After Main# #EndString#"

I would like to come up with a regular expression that can return all the text surrounded by the # symbols as matches. One of the things giving me grief is the fact that the # symbol is both the opening and closing delimiter. All of my attempts at a regex have just returned the entire string. The other issue is that it is possible for part of the string to not be surrounded by # symbols, as shown by the substring "Main String" above. Does anyone have any ideas? I have toyed around with Negative Look-behind assertion a bit, but haven't been able to get it to work. There may or may not be a space in between the groups of #'s but I want to ignore them (not match against them) if there are. The other option would be to just write a string parser routine, which would be fairly easy, but I would prefer to use a regex if possible.


Solution

  • [Edit]

    I think that this is what you need:

    (?<=#)[^#]+?(?=#)
    

    With input #First Thing# #Another One##No Space# Main String #After Main# matches:

    • First Thing
    • Another One
    • No Space
    • Main String
    • After Main

    The second match is the space between Thing# and #Another.

    [EDIT] To ignore space:

    (?<=)(?!\s+)[^#]+?(?=#)
    

    If you want to ignore trailing spaces:

    (?<=)(?!\s+)[^#]+?(?=\s*#)