How do I create a regex for this text? - java

I need to create a regex that checks if the text follows this format:
The first two letters will always be 'AB' than it will be a number
between 1-9 than either A or B than a dash ('-') than a bunch of
random text followed by a colon (':') and then index position that is
A letter and 2 digit number.
So like this:
AB8B-ANYLETTERS:H12
or
AB3B-ANYTHINGCANGOHERE:A77
I have done this to check the index position but cannot figure out the text before the colon.
"^.*:[A-H]\\d\\d"
So the general format is:
AB[1-9][A or B]-[ANYCHARACTERS]:[A-Z][01-99]
I am using Java.

I'm guessing that maybe this expression might validate that:
^AB[1-9][AB]-[^:]+:[A-Z][0-9]{2}$
The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.
Test
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "^AB[1-9][AB]-[^:]+:[A-Z][0-9]{2}$";
final String string = "AB8B-ANYLETTERS:H12\n"
+ "AB3B-ANYTHINGCANGOHERE:A77";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
RegEx Circuit
jex.im visualizes regular expressions:
Edit
For AC cases, we would try:
^AB[1-9][AC]-[^:]+:[A-Z][0-9]{2}$
Demo 2

Related

Regex to retrieve any alphanumeric otp code

I am trying to get a regex to retrieve alphanumeric OTP code (length of the code maybe dynamic i.e depending on user's choice) and must contain at least one digit.
I have tried the following regex :
"[a-zA-z][0-9].[a-zA-z]"
But if a special character is there in the code it should result null instead it retrieves the characters before and after the special character which is not desired.
Some sample OTP-containing messages on which the regex is desired to work successfully:
OTP is **** for txn of INR 78.90.
**** is your one-time password.
Hi, Your OTP is ****.
Examples of Alphanumeric OTPs with at least one-digit:
78784
aZ837
987Ny
19hd35
fc82pl
It would be a bit difficult, maybe this expression might work with an i flag:
[a-z0-9]*\d[a-z0-9]*
or with word boundaries:
(?<=\b)[a-z0-9]*\d[a-z0-9]*(?=\b)
Test
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "[a-z0-9]*\\d[a-z0-9]*";
final String string = "78784\n"
+ "aZ837\n"
+ "987Ny\n"
+ "19hd35\n"
+ "fc82pl";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.
Try this one
[0-9a-zA-Z]*[0-9]+[0-9a-zA-Z]*
This evaluates your desired result.
I have tested this in this site
Your regex is almost correct.
It should be \b[a-zA-z]*[0-9]+[a-zA-z0-9]*\b.

How can i split address in streetname and streetnumber in nifi?

I'm trying to split a given address (Muster Straße 114 a) in streetname and streetnumber. I'm working with nifi. The situation is the following: i have a FlowFile-Attribute (order_address) which has as FlowFile-content e.g Muster Straße 114 a, and i need to split it into sepereate attributes.
I tried
/\A\s*(?:?:\s*)?(\pN+[a-zA-Z]?(?:\s*[-\/\pP]\s*\pN+[a-zA-Z]?)*)\s*,?\s*(?P(?:[a-zA-Z]\s*|\pN\pL{2,}\s\pL)\S[^,#]*?(?<!\s))s*(?:(?:[,\/]|(?=\#))\s*(?!\s*\.(?P(?!\s).*?))? | ?:(?P.*?),\s*(?=.*[,\/]))??!\s*\.)(?P[^0-9#]\s*\S(?:[^,#](?!\b\pN+\s))*?(?<!\s))\s*[\/,]?\s*(?:\sNo[.:])?\s*(?P\pN+\s*-?[a-zA-Z]?(?:\s*[-\/\pP]?\s*\pN+(?:\s*[\-a-zA-Z])?)*|[IVXLCDM]+(?!.*\b\pN+\b))(?<!\s)\s*(?:(?:[,\/]|(?=\#)|\s)\s*(?!\s*No\.)\s*(?P(?!\s).*?))?)\s*\Z/xu
but it's not working for me
If we'd like to just separate our addresses into two parts, one including the digits and one without, we could find several expressions that'd cover this rule, such as:
(.*?)([\d].*)
Demo
Test
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "(.*?)([\\d].*)";
final String string = "Muster Straße 114 a";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
RegEx Circuit
jex.im visualizes regular expressions:
In nifi, you can use the Nifi Expression language to manipulate FlowFile-Attributes. So i used the UpadateAttribute-Processor, to create the new FlowFile-Attributes street_name and streed_number.
I used the replaceAll method with a simple regex, to get streetnumber and streetname.
^(\D*)(?:.*)
^\D*(.*)
This two regex did it.
Here you find the screenshot of the processor:

RegEx for matching special patterns

I'm trying to match a String like this:62.00|LQ+2*2,FP,MD*3 "Description"
Where the decimal value is 2 digits optional, each user is characterized by two Chars and it can be followed by
(\+[\d]+)? or (\*[\d]+)? or none, or both, or both in different order
like:
LQ*2+4 | LQ+4*2 | LQ*2 | LQ+8 | LQ
Description is also optional
What i have tried is this:
Pattern.compile("^(?<number>[\\d]+(\\.[\\d]{2})?)\\|(?<users>([A-Z]{2}){1}(((\\+[\\d]+)?(\\*[\\d]+)?)|((\\+[\\d]+)?(\\*[\\d]+)?))((,[A-Z]{2})(((\\+[\\d]+)?(\\*[\\d]+)?)|((\\+[\\d]+)?(\\*[\\d]+)?)))*)(\\s\\\"(?<message>.+)\\\")?$");
I need to get all the users so i can split them by ',' and then further regex my way into it.But i cannot grab anything out of it.The desired output from
62.00|LQ+2*2,FP,MD*3 "Description"
Should be:
62.00
LQ+2*2,FP,MD*3
Description
Accepted inputs should be of these kind:
62.00|LQ+2*2,FP,MD*3
30|LQ "Burgers"
35.15|LQ*2,FP+2*4,MD*3+4 "Potatoes"
35.15|LQ,FP,MD
The precise regex to match the inputs you described should be fulfilled by this regex,
^(\d+(?:\.\d{1,2})?)\|([a-zA-Z]{2}(?:(?:\+\d+(?:\*\d+)?)|(?:\*\d+(?:\+\d+)?))?(?:,[a-zA-Z]{2}(?:(?:\+\d+(?:\*\d+)?)|(?:\*\d+(?:\+\d+)?))?)*)(?: +(.+))?$
Where group1 will contain the number that can have optional decimals upto two digits and group2 will have the comma separated inputs as you described in your post and group3 will contain the optional description if present.
Explanation of regex:
^ - Start of string
(\d+(?:\.\d{1,2})?) - Matches the number which can have optional 2 digits after decimal and captures it in group1
\| - Matches literal | present in your input after the number
([a-zA-Z]{2}(?:(?:\+\d+(?:\*\d+)?)|(?:\*\d+(?:\+\d+)?))?(?:,[a-zA-Z]{2}(?:(?:\+\d+(?:\*\d+)?)|(?:\*\d+(?:\+\d+)?))?)*) - This part matches two letters followed by any combination of + followed by number and optionally having * followed by number OR * followed by number and optionally having + followed by number exactly either once or whole of it being optional and captures it in group2
(?: +(.+))? - This matches the optional description and captures it in group3
$ - Marks end of input
Regex Demo
I'm guessing that we have several optional groups here, that might not be a problem. The problem I'm having is that I'm not quite sure what would be the range of our inputs and what might be desired outputs.
RegEx 1
If we are just matching everything, that I'm guessing, we might like to start with something similar to:
[0-9]+(\.[0-9]{2})?\|[A-Z]{2}[+*]?([0-9]+)?[+*]?([0-9]+)?,[A-Z]{2},[A-Z]{2}[+*]?([0-9]+)?(\s+"Description")?
Here, we simply add a ? after every sub-expression that we wish to have it optional, then we use char lists and quantifiers, and start swiping everything from left to right, to cover all inputs.
If we like to capture, then we simply wrap any part that we want captured with a capturing group ().
Demo
Test
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "[0-9]+(\\.[0-9]{2})?\\|[A-Z]{2}[+*]?([0-9]+)?[+*]?([0-9]+)?,[A-Z]{2},[A-Z]{2}[+*]?([0-9]+)?(\\s+\"Description\")?";
final String string = "62.00|LQ+2*2,FP,MD*3 \"Description\"\n"
+ "62|LQ+2*2,FP,MD*3 \"Description\"\n"
+ "62|LQ+2*2,FP,MD*3\n"
+ "62|LQ*2,FP,MD*3\n"
+ "62|LQ+8,FP,MD*3\n"
+ "62|LQ,FP,MD";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
RegEx 2
If we wish to output three groups that is listed:
([0-9]+(\.[0-9]{2})?)\|([A-Z]{2}[+*]?([0-9]+)?[+*]?([0-9]+)?,[A-Z]{2},[A-Z]{2}[+*]?([0-9]+)?)(\s+"Description")?
Demo 2
Test
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "([0-9]+(\\.[0-9]{2})?)\\|([A-Z]{2}[+*]?([0-9]+)?[+*]?([0-9]+)?,[A-Z]{2},[A-Z]{2}[+*]?([0-9]+)?)(\\s+\"Description\")?";
final String string = "62.00|LQ+2*2,FP,MD*3 \"Description\"\n"
+ "62|LQ+2*2,FP,MD*3 \"Description\"\n"
+ "62|LQ+2*2,FP,MD*3\n"
+ "62|LQ*2,FP,MD*3\n"
+ "62|LQ+8,FP,MD*3\n"
+ "62|LQ,FP,MD";
final String subst = "\\1\\n\\3\\n\\7";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
RegEx 3
Based on updated desired output, this might work:
([0-9]+(\.[0-9]{2})?)\|((?:[A-Z]{2}[+*]?([0-9]+)?[+*]?([0-9]+)?,?)(?:[A-Z]{2}[+*]?([0-9]+)?[*+]?([0-9]+)?,?[A-Z]{2}?[*+]?([0-9]+)?[+*]?([0-9]+)?)?)(\s+"(.+?)")?
DEMO

Regex to match /src/main/{any_package}/*.java

I need a Regex for a set including only files from /src/main/java/{any_package}/*.java to apply CheckStyle rules only to these files in Eclipse.
All other files, e.g.: none *.java files, src/test/ should be ignored.
Maybe, this expression would also function here, even though your original expression is OK.
^\/src\/main\/java(\/[^\/]+)?\/\*\.java$
Demo 1
My guess is that here we wish to pass:
/src/main/java/{any_package}/*.java
/src/main/java/*.java
If the second one is undesired, then we would simply remove the optional group:
^\/src\/main\/java(\/[^\/]+)\/\*\.java$
Demo 2
and it might still work.
Test
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "^\\/src\\/main\\/java(\\/[^\\/]+)\\/\\*\\.java$";
final String string = "/src/main/java/{any_package}/*.java\n"
+ "/src/main/java/*.java";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
RegEx Circuit
jex.im visualizes regular expressions:
The regex I'm looking for is src[\/]main\/java\/?(?:[^\/]+\/?)*.java$
Regex 101 test demo

Java Regex to remove all full stops apart from real numbers within a text

I am trying to write a simple regex to remove all . apart from the ones which occur in real numbers.
E.g.
The value was 0.19 psi. The water level has to be brought to normal. Mtl.temp is going to be high..
The below regex selects all real numbers.
((\+|-)?([0-9]+)(\.[0-9]+)?)|((\+|-)?\.?[0-9]+)
I could do the other way wherein I could select for pattern wherein it selects . preceded by a word and succeed by space. But, the input test is not written in proper grammatical manner.
you can use the regex
\.(?!\d)
regex101 demo
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "\\.(?!\\d)";
final String string = ".12 . 0.123 Hi.there I am .invalid.";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}

Categories