Browsed by
Tag: regex

Remove all special characters except percentage (real time use case)?

Remove all special characters except percentage (real time use case)?

How to remove all special characters except percentage (real time use case)? If you are running a coupon website, then you will have this issue sometime while parsing/scrapping coupons from merchants.   For example, you will have these three types of strings: 1. Get 10% Cashback 2. Get Rs. 10/- Cashback 3. Get 10/- Cashback 4. Get (Indian rupee symbol) 10 Cashback If you use the below regex pattern then it will remove all the special characters: [ ](?=[ ])|[^-_,A-Za-z0-9]+…

Read More Read More

618 total views, no views today

Java Program to find two letter capital words using Regex

Java Program to find two letter capital words using Regex

Java Program to find two letter capital words in Java using Regex: import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexAppealAddress { public static void main(String[] args) { String input = "Javadomain is also called as JD which contains JAVA tutorials and programmings"; // This prints JD and JA // Pattern regexPattern = Pattern.compile("\\s[A-Z]{2,2}"); // This prints only JD Pattern regexPattern = Pattern.compile("\\s[A-Z]{2,2}\\s"); Matcher matcher = regexPattern.matcher(input); while (matcher.find()) { System.out.println(matcher.group()); } } } Output: JD Explanation: 1. \\s for whitespaces 2….

Read More Read More

501 total views, 1 views today

Java Regex Pattern to allow characters and spaces Example

Java Regex Pattern to allow characters and spaces Example

Java Regex Pattern to allow characters and spaces Example: Requirements: Java domain – Should be allowed – Should Not allowed Java – Should be allowed (Java) – Should Not allowed Javadomain – Should be allowed Java Regex Program to allow characters and whitespaces And also not allow only whitespaces: import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexSpace { public static void main(String[] args) { String input = " Java domain "; Pattern regex = Pattern .compile("^$|[~\\!\\@\\#\\$\\%\\^\\&\\.\\,\\*\\(\\)\\_\\+\\=\\{\\}\\|\\:\\\\’\\[\\]\\<\\>\\?\\/\\’\\^[0-9]]"); Matcher matcher = regex.matcher(input.trim()); if…

Read More Read More

777 total views, 1 views today

Java Regex Pattern Example

Java Regex Pattern Example

Regex [Regular Expression]: Regular expression is a useful one to match and extract only the required parts. Eg: Defining the phone number formats. Removing unreadable or unwanted characters from a string Sample Program: import java.util.regex.Matcher; import java.util.regex.Pattern; public class Regex { public static void main(String[] args) { String inputStr = "?Javadomain.in’s a technical,java-blog"; Pattern pattern = Pattern.compile("[\\s\\?\\,\\-\\’]+"); Matcher matcher = pattern.matcher(inputStr); boolean temp = matcher.find(); System.out.println(temp); if (temp) { String aftRegex = inputStr.replaceAll("[\\s\\?\\,\\-\\’]+", "_"); System.out.println(aftRegex); } else { System.out.println("else"); }…

Read More Read More

641 total views, 1 views today

Java regex to check empty end of url

Java regex to check empty end of url

Program Description: This program takes the end value from the url. Program: import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexEmptyEnd { public static String myurl = "http://ngdeveloper.com/regexpage&page=8372528"; public static void main(String[] args) { String regex = "page=(.*?)$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(myurl); if (matcher.find()) { String str = matcher.group(1); System.out.println("page is "+str); } } }   Output: page is 8372528 Thanks for reading this post………!!! 449 total views, 2 views today

449 total views, 2 views today