Where of my pattern is incorrect (Regex in Java)? - java

I have some string data that have some pattern like this:
"0x4F 0xBB 0x74 0xA9"
It has four parts.
Each part has three or four chars.
Each part begins with 0x.
Among the parts we have space.
Character just: A B C D E F
Numbers: 0-9
** i want to return this pattern as string ! how to return it ?
assume that sample input string that i get it, is :
"vhmbjfbfbbdbd This is part of my String 0x4F 0xBB 0x74 0xA9 and rest afhahjbsdvbskb"
how to return this string after matching: "0x4F 0xBB 0x74 0xA9"
String MP = MatchedPart(str).toString();
private static StringBuilder MatchedPart(String s) {
List<String> sarr = Arrays.asList(s.split(" 0x[A-F0-9]{1,2} 0x[A-F0-9]{1,2} 0x[A-F0-9]{1,2} 0x[A-F0-9]{1,2}"));
StringBuilder machpart = new StringBuilder();
for(String t : sarr) {
machpart = machpart.append(t) ;
}
return machpart ;
}
I write this function that gets a string and check pattern:
public static boolean test (String s) {
Pattern pattern = Pattern.compile(" 0x[ABCDEF0-9] 0x[ABCDEF0-9] 0x[ABCDEF0-9] 0x[ABCDEF0-9]");
Matcher matcher = pattern.matcher(s);
if (matcher.find()){
return true;
}
return false;
}

According to your question just use like as below:
public class PatternIncorrect {
public static void main(String[] args) {
System.out.println("------Test 1------------- ");
System.out.println(test1("0x4F 0xBB 0x74 0xA9"));
System.out.println("------Test 2--------------");
System.out.println(test2("0x4F 0xBB 0x74 0xA9"));
System.out.println("------Test 3--------------");
List<String> test3 = test3("0x4F 0xBB 0x74 0xA9asdasd0x4F 0xBB 0x74 0xA9mfdskf0x4F 0xBB 0x74 0xA9");
for(String t : test3) {
System.out.println(t);
}
}
public static boolean test1(String str) {
List<String> strings = Arrays.asList(str.split(" "));
boolean isMatch = true;
String regEx = "0x[1-9][ABCDEF]|0x[ABCDEF][1-9]|0x[1-9][1-9]|0x[ABCDEF][ABCDEF]";
for (String s : strings) {
if (!s.matches(regEx)) {
System.out.println(s);
isMatch = false;
break;
}
}
return isMatch;
}
public static boolean test2(String s) {
String regEx = "0x[A-F1-9]{1,2} 0x[A-F1-9]{1,2} 0x[A-F1-9]{1,2} 0x[A-F1-9]{1,2}";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(s);
return matcher.find();
}
public static List<String> test3(String s) {
String regEx = "0x[A-F1-9]{1,2} 0x[A-F1-9]{1,2} 0x[A-F1-9]{1,2} 0x[A-F1-9]{1,2}";
return Arrays.asList(s.split(regEx));
}
}
Source Code On enter link description here

Try
Pattern pattern = Pattern.compile(" 0x[ABCDEF1-9]{1,2} 0x[ABCDEF1-9]{1,2} 0x[ABCDEF1-9]{1,2} 0x[ABCDEF1-9]{1,2}");
If you want to be a bit more familiar with regex in Java, read : https://docs.oracle.com/javase/tutorial/essential/regex/index.html

Your expression starts with a white space, which, from what I am seeing, your input string does not.
Also, and more importantly, according to your description you are attempting to match 3 or 4 characters, but according to your expression you are always expecting 3.
Replace Pattern pattern = Pattern.compile(" 0x[ABCDEF1-9] 0x[ABCDEF1-9] 0x[ABCDEF1-9] 0x[ABCDEF1-9]"); with Pattern pattern = Pattern.compile("0x[A-F1-9]{1,2} 0x[A-F1-9]{1,2} 0x[A-F1-9]{1,2} 0x[A-F1-9]{1,2}"); and it should work.
For the record, 0x00 should also be a valid hexadecimal number, which your expression would seem to not consider as valid.

You can replace [ABCDEF1-9] with the predefined pattern \p{XDigit}
another suggestion:
Scanner sc = new Scanner(s);
int count = 0;
while(sc.hasNextInt(16)){
sc.nextInt(16);
count++;
}
return count == 4;

Related

Mask string with another pattern string

I'd like to mask numbers according to pattern. If number is 22123123123 and pattern is xxxxx***xxx the result of masking should be 22123***123. I wrote a code:
private String maskNumberWithPattern(String number) {
char[] pattern = "xxxxx***xxx".toCharArray();
char[] numberInput = number.toCharArray();
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < pattern.length; i++) {
if(pattern[i] == '*') {
stringBuffer.append("*");
} else {
stringBuffer.append(numberInput[i]);
}
}
return stringBuffer.toString();
}
Is there some standard api method to achieve that?
It's not clear from your code how your pattern behaves exactly. For example, what should happen if the input String is longer than your pattern? Is there a chance that the pattern does not match? Etc. I don't know such thing...
I guess wildcard patterns or regular expression is the easiest thing you can get here:
private static String maskNumberWithPattern(String number) {
Pattern pattern = Pattern.compile("(\\d{5})(\\d*)(\\d{3})");
Matcher matcher = pattern.matcher(number);
if (matcher.matches()) {
String group = matcher.group(2);
return matcher.group(1) + StringUtils.repeat('*', group.length()) + matcher.group(2);
}
else {
return number;
}
}

How can i check that my particular string has general punctuations? and how to remove it from string?

public static void main(String args[]) {
final StringBuilder builder = new StringBuilder();
String input = "Autism: 'Weak “Central Coherence” in: II. Real-life and MMag. ? ? ? ö ü ä André Gazsó";
final Pattern specialCharsForFieldContent = Pattern.compile("([-+\"!(){}\\[\\]^\\~\\: \\\\]|\\|\\||&&)");
for (char c : input.toCharArray()) {
Matcher m = specialCharsForFieldContent.matcher(input);
if (Character.isLetterOrDigit(c) || m.find()) {
builder.append(Character.isLowerCase(c) ? c : c);
}
}
System.out.println(builder.toString());
}
here before central there is punctuation its not a double quotes. I want to remove it.
please refer below link:
http://www.charbase.com/block/general-punctuation for puntuations.
Try This:
String words = input.replaceAll("[^a-zA-Z ]", "");

what is the proper regular expression for this example?

Which regular expression in java can do these conversions?
"1.54.0.21" to "01540021"
or
"33.5.10.6" to "33051006"
I need to replace .# with 0# and .## with ##
You could try something like...
StringBuilder output = new StringBuilder(8);
String input = "1.54.0.21";
Pattern p = Pattern.compile("\\d+");
Matcher matcher = p.matcher(input);
while (matcher.find()) {
String group = matcher.group();
if (group.length() < 2) {
output.append("0");
}
output.append(group);
}
System.out.println(input);
System.out.println(output);
Which outputs...
1.54.0.21
01540021
Without Regex :
http://rextester.com/LGXETU62790
public static void main(String args[])
{
String str1 = "33.5.9.6";
String str2 = "1.54.0.21";
System.out.println(transform(str1));
System.out.println(transform(str2));
}
private static String transform(String str){
String[] splitted = str.split("\\.");
StringBuilder build = new StringBuilder();
for(String s : splitted){
build.append(String.format("%02d", Integer.parseInt(s)));
}
return build.toString();
}
The only functionality of a regular expression is to match a certain pattern of characters inside a string (or multiline strings).
A regular expression can be used in a find and replace Pattern but only to find the strings you are interested in. When they are found , a Split(), Remove(), Replace(), function will better do it's purpose.
I recommend you : http://gskinner.com/RegExr/
This is an online tool for matching strings with regular expression, and also learning the patterns.
public String getToken(String elem) {
return (elem.size() == 1) ? ("0" + elem) : elem;
}
String[] a = "1.54.0.21".split("\\.");
String o = "", e;
int i = 0, len = a.size();
for (i = 0; i < len; i++) {
o = o + getToken(a[i]);
}
System.out.println(o); //01540021

Is it possible to use the number of current replacement in String.replaceAll?

Is it possible to make String.replaceAll put the number (count) of the current replacement into the replacement being made?
So that "qqq".replaceAll("(q)", "something:$1 ") would result in "1:q 2:q 3:q"?
Is there anything that I can replace something in the code above with, to make it resolve into the current substitution count?
Here is one way of doing this:
StringBuffer resultString = new StringBuffer();
String subjectString = new String("qqqq");
Pattern regex = Pattern.compile("q");
Matcher regexMatcher = regex.matcher(subjectString);
int i = 1;
while (regexMatcher.find()) {
regexMatcher.appendReplacement(resultString, i+":"+regexMatcher.group(1)+" ");
i++;
}
regexMatcher.appendTail(resultString);
System.out.println(resultString);
See it
No, not with the replaceAll method. The only backreference is \n where n is the n'th capturing group matched.
For this you have to create your own replaceAll() method.
This helps you:
public class StartTheClass
{
public static void main(String[] args)
{
String string="wwwwww";
System.out.println("Replaced As: \n"+replaceCharector(string, "ali:", 'w'));
}
public static String replaceCharector(String original, String replacedWith, char toReplaceChar)
{
int count=0;
String str = "";
for(int i =0; i < original.length(); i++)
{
if(original.charAt(i) == toReplaceChar)
{
str += replacedWith+(count++)+" ";//here add the 'count' value and some space;
}
else
{
str += original.charAt(i);
}
}
return str;
}
}
The output I got is:
Replaced As:
ali:0 ali:1 ali:2 ali:3 ali:4 ali:5

How can I count the number of matches for a regex?

Let's say I have a string which contains this:
HelloxxxHelloxxxHello
I compile a pattern to look for 'Hello'
Pattern pattern = Pattern.compile("Hello");
Matcher matcher = pattern.matcher("HelloxxxHelloxxxHello");
It should find three matches. How can I get a count of how many matches there were?
I've tried various loops and using the matcher.groupCount() but it didn't work.
matcher.find() does not find all matches, only the next match.
Solution for Java 9+
long matches = matcher.results().count();
Solution for Java 8 and older
You'll have to do the following. (Starting from Java 9, there is a nicer solution)
int count = 0;
while (matcher.find())
count++;
Btw, matcher.groupCount() is something completely different.
Complete example:
import java.util.regex.*;
class Test {
public static void main(String[] args) {
String hello = "HelloxxxHelloxxxHello";
Pattern pattern = Pattern.compile("Hello");
Matcher matcher = pattern.matcher(hello);
int count = 0;
while (matcher.find())
count++;
System.out.println(count); // prints 3
}
}
Handling overlapping matches
When counting matches of aa in aaaa the above snippet will give you 2.
aaaa
aa
aa
To get 3 matches, i.e. this behavior:
aaaa
aa
aa
aa
You have to search for a match at index <start of last match> + 1 as follows:
String hello = "aaaa";
Pattern pattern = Pattern.compile("aa");
Matcher matcher = pattern.matcher(hello);
int count = 0;
int i = 0;
while (matcher.find(i)) {
count++;
i = matcher.start() + 1;
}
System.out.println(count); // prints 3
This should work for matches that might overlap:
public static void main(String[] args) {
String input = "aaaaaaaa";
String regex = "aa";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
int from = 0;
int count = 0;
while(matcher.find(from)) {
count++;
from = matcher.start() + 1;
}
System.out.println(count);
}
From Java 9, you can use the stream provided by Matcher.results()
long matches = matcher.results().count();
If you want to use Java 8 streams and are allergic to while loops, you could try this:
public static int countPattern(String references, Pattern referencePattern) {
Matcher matcher = referencePattern.matcher(references);
return Stream.iterate(0, i -> i + 1)
.filter(i -> !matcher.find())
.findFirst()
.get();
}
Disclaimer: this only works for disjoint matches.
Example:
public static void main(String[] args) throws ParseException {
Pattern referencePattern = Pattern.compile("PASSENGER:\\d+");
System.out.println(countPattern("[ \"PASSENGER:1\", \"PASSENGER:2\", \"AIR:1\", \"AIR:2\", \"FOP:2\" ]", referencePattern));
System.out.println(countPattern("[ \"AIR:1\", \"AIR:2\", \"FOP:2\" ]", referencePattern));
System.out.println(countPattern("[ \"AIR:1\", \"AIR:2\", \"FOP:2\", \"PASSENGER:1\" ]", referencePattern));
System.out.println(countPattern("[ ]", referencePattern));
}
This prints out:
2
0
1
0
This is a solution for disjoint matches with streams:
public static int countPattern(String references, Pattern referencePattern) {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
new Iterator<Integer>() {
Matcher matcher = referencePattern.matcher(references);
int from = 0;
#Override
public boolean hasNext() {
return matcher.find(from);
}
#Override
public Integer next() {
from = matcher.start() + 1;
return 1;
}
},
Spliterator.IMMUTABLE), false).reduce(0, (a, c) -> a + c);
}
Use the below code to find the count of number of matches that the regex finds in your input
Pattern p = Pattern.compile(regex, Pattern.MULTILINE | Pattern.DOTALL);// "regex" here indicates your predefined regex.
Matcher m = p.matcher(pattern); // "pattern" indicates your string to match the pattern against with
boolean b = m.matches();
if(b)
count++;
while (m.find())
count++;
This is a generalized code not specific one though, tailor it to suit your need
Please feel free to correct me if there is any mistake.

Categories

Resources