简体   繁体   中英

How to identify a word with a dollar sign $

I am trying to extract information based on if a string starts with a dollar sign.

I have a string "I bought $BTC"

I am trying to get if word in string starts with dollar sign, get me the word. So I am trying to get in this case "BTC". I tried, string.replace methods and findall, however cannot extract this information. I further read that dollar sign is used as interpolation in Python, I am assuming this is causing the issue.

Are there any ideas that can help me with this?

Try this for your regex:

(?<=\$)\w+

It will capture any alphanumeric characters after the $. You can experiment your regex using pythex.org

Following code blocks works for me - returns "BTC". Regex in question is "\\$(\\w+)"

import re

test = "I bought $BTC today"
result = re.search("\$(\w+)", test)
print result.group(1)

Group(1) specifies that you're after the characters within the brackets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM