Question

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.

Was it helpful?

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*#)

OTHER TIPS

/((#[^#]+#)|([^#]+))/

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top