ในบทความนี้ เราจะทำความเข้าใจวิธีพิมพ์อักษรตัวแรกของแต่ละคำโดยใช้ regex นิพจน์ทั่วไปคือลำดับของอักขระที่สร้างรูปแบบการค้นหา นิพจน์ทั่วไปอาจเป็นอักขระตัวเดียวหรือรูปแบบที่ซับซ้อนกว่าก็ได้
นิพจน์ทั่วไปช่วยให้คุณจับคู่หรือค้นหาสตริงหรือชุดของสตริงอื่นๆ โดยใช้ไวยากรณ์เฉพาะที่จัดอยู่ในรูปแบบ สามารถใช้เพื่อค้นหา แก้ไข หรือจัดการข้อความและข้อมูลได้
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
สมมติว่าข้อมูลที่เราป้อนคือ −
Input String_1: Java Program Input String_2: Joy of learning
ผลลัพธ์ที่ต้องการจะเป็น −
Result_1: JP Result_2: Jol
อัลกอริทึม
Step 1 - START Step 2 - Declare two string values namely input_string_1 and input_string_2. Declare a regex Pattern namely string_pattern and a Matcher object namely string_matcher. Step 3 - Define the values. Step 4 - Using a while-loop, compute string_matcher.group() to fetch the first letter of each word. Step 5 - Display the result Step 6 - Stop
ตัวอย่างที่ 1
ที่นี่ เรารวมการดำเนินการทั้งหมดเข้าด้วยกันภายใต้ฟังก์ชัน 'หลัก'
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
public static void main(String[] args) {
System.out.println("Required packages have been imported");
String input_string_1 = "Java Program";
System.out.println("\nThe first string is defined as: " +input_string_1);
Pattern string_pattern = Pattern.compile("\\b[a-zA-Z]");
Matcher string_matcher = string_pattern.matcher(input_string_1);
System.out.println("The first letters of the string is : ");
while (string_matcher.find())
System.out.print(string_matcher.group());
System.out.println();
String input_string_2 = "Joy of learning";
System.out.println("\nThe second string is defined as: " +input_string_2);
Matcher string_matcher_2 = string_pattern.matcher(input_string_2);
System.out.println("The first letters of the string is : ");
while (string_matcher_2.find())
System.out.print(string_matcher_2.group());
System.out.println();
}
} ผลลัพธ์
Required packages have been imported The first string is defined as: Java Program The first letters of the string is : JP The second string is defined as: Java Program The first letters of the string is : Jol
ตัวอย่างที่ 2
ในที่นี้ เราสรุปการดำเนินการเป็นฟังก์ชันที่แสดงการเขียนโปรแกรมเชิงวัตถุ
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
static void print_regex_string(String s) {
Pattern string_pattern = Pattern.compile("\\b[a-zA-Z]");
Matcher string_matcher = string_pattern.matcher(s);
System.out.println("The first letters of the string is : ");
while (string_matcher.find())
System.out.print(string_matcher.group());
System.out.println();
}
public static void main(String[] args) {
System.out.println("Required packages have been imported");
String input_string_1 = "Java Program";
System.out.println("\nThe first string is defined as: " +input_string_1);
print_regex_string(input_string_1);
String input_string_2 = "Joy of learning";
System.out.println("\nThe second string is defined as: " +input_string_1);
print_regex_string(input_string_2);
}
} ผลลัพธ์
Required packages have been imported The first string is defined as: Java Program The first letters of the string is : JP The second string is defined as: Java Program The first letters of the string is : Jol