Regex tag generator in Java

Regex tag generator in Java

ยท

5 min read

Hi there everyone. ๐Ÿ‘‹ How's your weekend going?
Hope you are doing well. ๐Ÿ™‚

So, as you read the title, I am gonna share with you about creating a tag generator using Regex in Java.

Prerequisites ๐ŸŽ’

  • A bit of knowledge of Java,
  • A bit of knowledge of Regex,
  • Willing to code.

Coding โ˜•๏ธ

public class tag {

}
  • A class will be created when we will start creating a .java file.
  • I have given its name as tag.java

import java.util.Scanner;
public class tag {

}
  • Now we will import the java.util.Scanner package.

import java.util.Scanner;
public class tag {

    public static void main(String[] args) {

    }
}
  • Next, we will add main() inside of which, we will insert our regex equation and input-output method.

import java.util.Scanner;
public class tag {
    private static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        System.out.println("Insert words to get their tags: \n");
        String words = sc.nextLine();

    }
}
  • In this step, we add the Scanner() to get input and System.out.print() to display a message, as shown in the above code snippet.

import java.util.Scanner;
public class tag {
    private static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        System.out.println("Insert words to get their tags: \n");
        String words = sc.nextLine();
        String regex = "^[0-9a-zA-Z\s][a-zA-Z\s][a-zA-Z0-9\s]+$";

    }
}
  • Here, we are going to add our **regex** equation.
  • In this equation:
    • First bracket [] is used to get:
      • Any number from 0 to 9,
      • And any character from a to z with any of the upper or lower case.
    • Second bracket [] is used to get:
      • Any character from a to z and A to z.
    • Third bracket [] is used to get:
      • Any character from a to z and A to Z with numbers from 0 to 9.
    • All the three brackets have \s to allow a blank space.
    • The caps ^ and dollar sign $ are start and end of the equation.

Final code: โœŒ๐Ÿผ

import java.util.Scanner;
public class tag {
    private static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        System.out.println("Insert words to get their tags: \n");
        String words = sc.nextLine();
        String regex = "^[0-9a-zA-Z\s][a-zA-Z\s][a-zA-Z0-9\s]+$";
        if (words.matches(regex)) {
            String tags = words.replaceAll("(?)([ ])", "$1#");
            System.out.println("#" + tags);
        } else {
            System.out.println("Try something else.");
        }
    }
}
  • At last, we will add two things.
    1. if(){}else{} statement to validate and output the required tags.
    2. replaceall() as shown in the code to replace blank space with a combination of space plus #, just before every word it will get as input.

Output: ๐Ÿ†

Screenshot 2020-11-29 at 8.50.51 PM.jpg

  • Here, when we add multiple words with space between them, it will return them with each word having a hashtag with it.
  • Thus, we don't need to add the # sign every time while typing a word for multiple hashtags.

So, this is how we can create a tag generator using Regex in Java.
By the way, feel free to share a screenshot if you try this cool project.

Hope you like this article/tutorial of my series Random-Coding.
Please read my other articles of this series as well for more cool and fun projects.

I will meet you through my next article soon. Till then, stay connected with me through Twitter and Discord

Keep Coding, keep growing โ˜•๏ธ

References: ๐Ÿ“š

image.png