How to use Regex in Java for pattern matching?

I read the documentation and various tutorials online, but I'm still confused about how regex works in Java. What I'm trying to do is create a function that takes an argument of type string. Then I want to check if the passed string contains any characters except MDCLXVIivxlcdm. So, for example, the string "XMLVID" should return false, and "ABXMLVA" should return true.

public boolean checkString(String arg) { Pattern p = Pattern.complile("[a-zA-z]&&[^MDCLXVIivxlcdm]"); Matcher m = p.matcher(arg); if(m.matches()) return true; else return false; } 

When I pass in "XMLIVD", "ABXMLVA" and "XMLABCIX", all return false. What am I doing wrong? Any help would be greatly appreciated.

+3
source share
3 answers

You will need to use the Java character class operator in the character class, otherwise it literally matches && . Btw, your first character class from A to (lowercase) z also includes [\]^_ , which you certainly don't want; and you mistakenly wrote "Patter.complile".

In addition, matches()

Trying to match the entire area with a pattern.

Thus, you need to either use find() or substitute the expression with .* .

 public boolean checkString(String arg) { return Pattern.compile("[[a-zA-Z]&&[^MDCLXVIivxlcdm]]").matcher(arg).find(); } 
+6
source

you can use such a function using two arguments, namely:

  • origingalString source string to check
  • searchString string

the code is exactly

 public boolean checkCompletelyExist(String origingalString,String searchString){ boolean found = false; String regex = ""; try{ for(int i = 0; i < searchString.length();i++){ String temp = String.valueOf(searchString.charAt(i)); regex = "[\\x20-\\x7E]*"+"["+temp.toLowerCase()+"|"+temp.toUpperCase()+"]+[\\x20-\\x7E]*"; if(!origingalString.matches(regex)){ found = true; break; } } System.out.println("other character present : "+found); } catch (Exception e) { e.printStackTrace(); } return found; } 

eg:

checkCompletelyExist("MDCLXVIivxlcdm","XMLVID") will be other character present : false

and

checkCompletelyExist("MDCLXVIivxlcdm","ABXMLVA") output will be other character present : true

+1
source

Regular Expressions (RegEx / RegExp) Basically, a regular expression is a template that describes a certain amount of text.

 ^abc$ start / end of the string \b \B word, not-word boundary \w \d \s word, digit, whitespace \W \D \S not word, digit, whitespace \z - End of entire string (…) - Grouping (capture groups) [abc] any of a, b, or c [^abc] not a, b, or c [ag] character between a & g { m,n } - quantifiers for "from m to n repetitions" + - quantifiers for 1 or more repetitions (ie, {1,}) ? - quantifiers for 0 or 1 repetitions (ie, {0,1}) 

POSIX Bracket Expressions POSIX brackets are a special type of character class. POSIX parenthesis expressions correspond to a single character in a character set, just like regular character classes. POSIX character class names must be lowercase. The POSIX standard defines 12 character classes. The table below lists all 12, plus the classes [:ascii:] and [:word:] , which also support some regex options.

enter image description here

Regular Expression Matching:

 final Pattern mix_alphaNumaric_pattern = Pattern.compile("^[A-Za-z0-9]+$"); final Pattern alphabets_pattern = Pattern.compile("^[A-Za-z,\\- ]+$"); final Pattern alphabetsNull_pattern = Pattern.compile("|^[A-Za-z,\\- ]+$"); final Pattern numaric_pattern = Pattern.compile("^[0-9]+$"); // ^begning +followed By $end final Pattern date_time_pattern = Pattern.compile("\\d{1,2}/\\d{1,2}/\\d{4}\\s\\d{1,2}\\:\\d{1,2}"); final Pattern weather_pattern = Pattern.compile("[\\-]*\\d{1,3}\\.\\d{1,6}"); final Pattern email_pattern = Pattern.compile("^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$"); final Pattern mobile_pattern = Pattern.compile("(\\+)?(\\d{1,2})?\\d{10}"); public static void main(String[] args) { String[] str = {"MCDL", "XMLIVD", "ABXMLVA", "XMLABCIX"}; Pattern p = Pattern.compile("^(M|D|C|L|X|V|I|i|v|x|l|c|d|m)+$"); // Returns: true if, and only if, the entire region sequence matches this matcher pattern for (String sequence : str ) { boolean match = false, find = false; if ( !p.matcher(sequence).matches() ) match = true; if (p.matcher(sequence).find()) find = true; System.out.format("%s \t Match[%s] Find[%s]\n", sequence, match, find); } } 

Output:

 MCDL Match[false] Find[true] XMLIVD Match[false] Find[true] ABXMLVA Match[true] Find[false] XMLABCIX Match[true] Find[false] 

@ All link to:

0
source

All Articles