2

I am having issue with a reg ex expression and can't find the answer to my question.

I am trying to build a reg ex pattern that will pull in any matches that have # around them. for example #match# or #mt# would both come back.

This works fine for that. #.*?# However I don't want matches on ## to show up. Basically if there is nothing between the pound signs don't match.

Hope this makes sense. Thanks.

4
  • Is it assumed that there will be no spaces inside of the hashes? Commented Mar 4, 2015 at 23:08
  • What would be##matched##in##this##and##that###or#the#other#thing# ? So, it looks like you left out %90 of it. Commented Mar 4, 2015 at 23:13
  • Please see my edit below Commented Mar 4, 2015 at 23:20
  • @sln 89.79591836734694% to be precise :) Commented Mar 4, 2015 at 23:48

3 Answers 3

5

Please use + to match 1 or more symbols:

#+.+#+

UPDATE: If you want to only match substrings that are enclosed with single hash symbols, use:

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

See regex demo

Explanation:

  • (?<!#)#(?!#) - a # symbol that is not preceded with a # (due to the negative lookbehind (?<!#)) and not followed by a # (due to the negative lookahead (?!#))
  • [^#]+ - one or more symbols other than # (due to the negated character class [^#])
  • #(?!#) - a # symbol not followed with another # symbol.
Sign up to request clarification or add additional context in comments.

3 Comments

I tried that and get matches in between. This entire line then matched ##test## matches. #test#
Please edit your question to include some concrete examples of the input strings and the desired output. It is not easy to make guesses with regexes.
I have updated the regex, it works a bit differently from the one accepted as it wouldn't match #mixed# in #mixed##.
2

Instead of using * to match between zero and unlimited characters, replace it with +, which will only match if there is at least one character between the #'s. The edited regex should look like this: #.+?#. Hope this helps!

Edit

Sorry for the incorrect regex, I had not expected multiple hash signs. This should work for your sentence: #+.+?#+

Edit 2

I am pretty sure I got it. Try this: (?<!#)#[^#].*?#. It might not work as expected with triple hashes though.

4 Comments

When I ran that on this sentence from a reg ex site. Edit the Expression & Text to ##see## matches. #Roll# over matches or the expression for details. I see this as matching ##see## matches. #
Do you want the text between the double hash signs to show, or not come up at all?
I need everything to show. So for example #content# would return "#content" but ## would be ignored
Close, but still getting matches when ##match## is used fore example
1

Try:

[^#]?#.+#[^#]?

The [^ character_group] construction matches any single character not included in the character group. Using the ? after it will let you match at the beginning/end of a string (since it matches the preceeding character zero or more times. Check out the documentation here

1 Comment

Perhaps I didn't read your question correctly, are looking for matches on ##string## or do those need kicked out?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.