Reputation: 880
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.
Upvotes: 1
Views: 78
Reputation: 31461
/((#[^#]+#)|([^#]+))/
Perhaps something like the above will match what you want.
This will match the space in between two hashes. Hmm.
/((#[^#]+#)|([^#]*[^#\s]+[^#]*))/
That will get rid of the nasty space, I think.
Upvotes: 2
Reputation: 3743
Try this. The first and last groups should not be captured and the .*? should be lazy
(?:#)(.*?)(?:#)
I think this is what you really need:
((#[^#]+#)|([^#]*[^#\s]+[^#]*))
but it will not capture the #
's around Main String
Upvotes: 1
Reputation: 78487
[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*#)
Upvotes: 1