Check if string contains CJK (chinese) characters [duplicate] - java

This question already has answers here:
Use regular expression to match ANY Chinese character in utf-8 encoding
(7 answers)
Closed 9 years ago.
I need to check if a string contains chinese characters.
After searching i found that i have to look with the regex on this pattern \u31C0-\u31EF,
But i don't manage to get the regex work.
Anyone experienced with this situation ? is the regex correct ?

As discussed here, in Java 7 (i.e. regex compiler meets requirement RL1.2 Properties from UTS#18 Unicode Regular Expressions), you can use the following regex to match a Chinese (well, CJK) character:
\p{script=Han}
which can be appreviated to simply
\p{Han}

Related

Replacing Regular expression matches in Java [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I want to replace &sp; in the string below with Z.
Input text : ABCD&sp;EF&p;GHIJ&bsp;KL
Output text : ABCDZEFZGHIZKL
Can anyone tell me how to replace the every instance of &\D+; using java regular expression?
I am using /(&\D+;)?/ but it doesn't work.
Use String#replaceAll.
You also should use the ? modificator to +:
String str = "ABCD&sp;EF&p;GHIJ&bsp;KL";
String regex = "&\\D+?;";
System.out.println (str.replaceAll(regex,"Z"));
This should work
Match the initial &, then all characters that are not the tailing ;, then that tailing ; like so: &[^;]+; If not matching numbers (as suggested by your example with \D) is a requirement, add the numbers to the negated character set: [^;0-9] To make it replace all occurrences, add the global flag g. The site regexr.com is a handy tool to create regexes.
Edit: Sorry, I initially read your question wrong.

Convert regex into java regex [duplicate]

This question already has answers here:
Regexp Java for password validation
(17 answers)
Closed 5 years ago.
string should not be longer than 26 alphanumeric characters
string should not begin with www OR api OR admin
string may contain hyphens
I have this regular expression that works:
^(?!www)(?!admin)(?!api)[a-zA-Z0-9.]{1,26}
Can you help me convert that regex into a java style string regex?
I found the answer by changing my regex to the following:
^(www|api|admin)\w{1,26}$

Escape Sequence vs. Whitespace Character (\s) [duplicate]

This question already has answers here:
Version difference? Regex Escape in Java
(2 answers)
Closed 1 year ago.
Are escape sequences and whitespace characters the same thing? I'm not sure what else to write here but Stackoverflow said the first sentence is not enough so I'm typing this second sentence for no reason at all but that so this post will go through.
There are a few escape sequences specified in Java, of which \s is not part. The \s is recognized as whitespace in regular expressions, where it is a predefined character class.
Check the following sections from the Java Tutorial:
Escape Sequences
Predefined Character Classes

Java regex for Uk postcodes with spaces [duplicate]

This question already has answers here:
RegEx for matching UK Postcodes
(33 answers)
Closed 7 years ago.
I'm trying to create a regex matching the following patterns (with and without space):
M1 1AA, M60 1NW, CR2 6XH, DN55 1PT, W1A 1HQ and EC1A 1BB
I'm very new at this and find it hard to create a functional regex for all the examples above.
Searching here and there I found a regex that might work for some of the patterns but I don't know how to add the condition "with or without space" for each type of postcode.
Here the regex I found on another post "^(A-PR-UWYZ [0-9][ABD-HJLNP-UW-Z]{2})"
How do I add the space/no space condition? In order to match M11AA or M1 1AA.
You need this regex:
^([A-PR-UWYZ](([0-9](([0-9]|[A-HJKSTUW])?)?)|([A-HK-Y][0-9]([0-9]|[ABEHMNPRVWXY])?)) ?[0-9][ABD-HJLNP-UW-Z]{2})$
^
This space must be set as optional with ? quantifier that means 0 or 1 repetition.
See demo

Java Regular expression exclude special characters [duplicate]

This question already has answers here:
Regular expression for excluding special characters [closed]
(11 answers)
Closed 9 years ago.
I have to validate a String to check if it contains a special character or not. This string may contain any number and words ( including unicode, ie: à, â,ô .. ) but should not accept any special characters ( ie: !,#,#,%,^ ... )
Sorry for my bad English.
This character class
[\p{L}\p{No}\p{Space}]
will include all characters which Unicode declares as either "letters", "numbers", or "whitespace characters". If you want to match a string against such a character class, you would write the following:
input.matches("[\\p{L}\\p{No}\\p{Space}]+")
For future reference, I have extracted all this information from the java.util.Pattern class. You should refer to that page for all your future interest in the Java regular expressions.
You can try [\\p{L}\\s]+. as example. This will remove special characters.
Pattern p=Pattern.compile("[\\p{L}\\s]+");
Matcher m=p.matcher("hissd#");
if(m.find()){
System.out.println(m.group(0));//
}

Categories