Social
Cashtag (stock ticker like $AAPL) Regex
A cashtag is a dollar sign followed by a stock ticker, such as $AAPL, used on X, StockTwits and trading forums to tag posts about a company. This pattern finds a $ at the start of text or after whitespace, followed by one to five capital letters and an optional share-class suffix like .B. Because it requires letters, prices such as $100 are ignored, and lowercase text is skipped. It is useful for sentiment analysis, market chatter dashboards and tagging finance content. It cannot confirm that a ticker is listed on any exchange, and it misses tickers that contain digits.
Pattern (JavaScript)
/(?:^|\s)\$[A-Z]{1,5}(?:\.[A-Z])?\b/Matches (the start of the line/string OR a whitespace character (space, tab, newline)), then the character "$", then an uppercase letter, between 1 and 5 times, then (the character ".", then an uppercase letter), optionally (zero or one time), then a word boundary.
Examples
Matches
- ✓ Watching $AAPL today
- ✓ $TEST
- ✓ Long $BRK.B and $XYZ
- ✓ $A
Does not match
- ✗ $100 off
- ✗ $aapl
- ✗ costs US$5
- ✗ $TOOLONG
Try it
5 matches (JavaScript engine)
Watching $AAPL today $TEST Long $BRK.B and $XYZ $A $100 off $aapl costs US$5 $TOOLONG
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /(?:^|\s)\$[A-Z]{1,5}(?:\.[A-Z])?\b/g;
const text = "Watching $AAPL today";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'(?:^|\s)\$[A-Z]{1,5}(?:\.[A-Z])?\b')
text = 'Watching $AAPL today'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/(?:^|\s)\$[A-Z]{1,5}(?:\.[A-Z])?\b/';
$text = 'Watching $AAPL today';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("(?:^|\\s)\\$[A-Z]{1,5}(?:\\.[A-Z])?\\b");
String text = "Watching $AAPL today";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?:^|\s)\$[A-Z]{1,5}(?:\.[A-Z])?\b`)
text := "Watching $AAPL today"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"(?:^|\s)\$[A-Z]{1,5}(?:\.[A-Z])?\b");
var text = "Watching $AAPL today";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"(?:^|\s)\$[A-Z]{1,5}(?:\.[A-Z])?\b").unwrap();
let text = "Watching $AAPL today";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /(?:^|\s)\$[A-Z]{1,5}(?:\.[A-Z])?\b/
text = 'Watching $AAPL today'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why are prices like $100 not matched?
- Cashtags use letters, while prices use digits. Requiring a capital letter right after the $ cleanly separates tickers from money amounts in the same sentence.
- What is the .B in $BRK.B?
- Some companies have several share classes, and a dot suffix marks the class. The pattern allows one optional dot and capital letter after the main ticker to cover these.
- Does it work for crypto tickers like $BTC?
- Yes, because $BTC has the same shape as a stock cashtag. If you need to tell crypto and stock tickers apart, compare the captured symbol against a list in code.
Related patterns
- @mention(?:^|[^\w@.])@[A-Za-z0-9_]{1,30}\b
- Discord invite link^(?:https?://)?(?:www\.)?(?:discord\.gg|discord(?:app)?\.com/invite)/[A-Za-z0-9-]{2,32}/?$
- Facebook page URL^(?:https?://)?(?:www\.|m\.)?(?:facebook|fb)\.com/[A-Za-z0-9.]{5,50}/?$
- GitHub repository URL^(?:https?://)?(?:www\.)?github\.com/[A-Za-z0-9](?:-?[A-Za-z0-9]){0,38}/[A-Za-z0-9._-]+?(?:\.git)?/?$