Reputation: 5196
I need to find out all words in a sentence that are between a $ and a space like this this is $abc $cde any $ety.
The result should be abc, cde and ety.
I tried this
'(?<=$$)(.*)(?=)'
but it shows some error. What is wrong in this or any new suggestions?
Upvotes: 0
Views: 63
Reputation: 15975
You can try this:
\$(\w+)
As capturing groups, you'll get each of the words.
\w
will match a-Z, 0-9 and _, if you want to match only letters, for instance, you can change to: \$([a-zA-Z]+)
Upvotes: 1
Reputation: 5764
Assuming from the question, each word (word contains only chars A-Za-z) must begin with $
and have a space at the end.
The following regex will match such words -- \$([A-Za-z])+
(there is a space at the end, which is hard to see due to the formatting here). If there are multiple spaces, you can use +
(space before +, hard to see again due to formatting) at the end of the regex.
Then you can extract the first matching group (i.e. $1
) as your matching word, and you need to do this in a loop till there are no more matches you can extract. That is something like --
while ($x =~ /\$([A-Za-z])+ /g) {
// $1 is your match
}
If your word contains more than just chars, then you can use \w
as mentioned by pcalcao
, which will include both 0-9
and _
Upvotes: 0