Reputation: 3855
Consider the following string
'#PI + #C + 1.2'
i am trying to get each word after the hash but also ignore the hash in the result
i use
#(\S+)
which is fine but i am not sure how to make sure the hash is there but ignore it in the result
Upvotes: 0
Views: 432
Reputation: 907
I think your answer works, here is how I would go about it:
#([^\s]+)\s*
Which reads like: Match a "#", then capture all non-space chars into the first capture group (there has to be at-least one non-space char). After that, match an arbitrary number of spaces.
The entire reg-ex would not "match" or evaluate to true, unless the "#" is found before a bunch of non-whitespace chars.
Obviously, if you are guaranteed of the form #XXX + #YYY + CCC
, you can construct a much more targeted reg-ex to pick the appropriate values into the correct capture groups.
Edit:
Just noticed that you said "the # shows up in the result", the "result" of what? Typically the regex match object will return the entire matched string, if you are looking to get the contents of (...)
- you will have to access the appropriate capture group of the matched regex. And that is very language dependent. In python you can do:
re_SOMETHING = re.compile("#([^\s]+)\s*", ...)
match = re_SOMETHING.match("#PI + #C + 1.2")
if match:
pi = match.group(1)
# pi === "PI"
Upvotes: 1
Reputation: 2306
I don't know exactly what you mean by ignoring the # in the result. Your regex is doing just that. It matches the # to make sure it's there and then only captures the stuff after it. If you don't want the # to match at all you have to use zero-width assertions or look arounds:
(?<=#)(\S+)
Upvotes: 1