Java Regex for Trademark Symbol Validation

In this Java regex example, we will learn to match trademark symbol ™ in a string using the regular expression.

1. Regex for Trademark Symbol

The Unicode code point U+2122 represents the “trademark sign” character. You can match this with “\u2122”, “\u{2122}”, or “\x{2122}”, depending on the regex flavor you’re working with.

\u2122

2. Example

Let us see an example of how to use the above regex to validate a string it if contains the trademark symbol or not. The following program searches the occurrence of the trademark symbol using regex and prints its location in then string using the start and end indices.

String string = "Searching in trademark character ™ is so easy when you know it.";
   
String regex = "\u2122";
 
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(string);

while (matcher.find()) {

   System.out.print("Start index: " + matcher.start());
   System.out.print(" End index: " + matcher.end() + " ");
   System.out.println(matcher.group());
}

The program output:

Start index: 33 End index: 34 

™

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode