Java regex - allow only if n digits in group from long string - java

i need a regex which allows a string, unless there are 6 or more numbers in a group at any point.
my current wrong regex:
^([a-zA-Z ]*)|(\d{0,5})$
match:
teststring 12345
teststring
1234 teststring
teststring 123 teststring
test1234string
not match:
1234567 teststring
teststring 123456
test123456789string
i hope someone can help.
thx guys
UPDATE:
this regex does the job:
^(?!.*\d{6}).*$
thx #WiktorStribiżew

The pattern to match a string that has no 6 consecutive digits is
^(?!.*\d{6}).*
The regex demo is available here. If there can be line breaks inside, you need to add a DOTALL modifier that will make . match all chars including line break chars: (?s)^(?!.*\d{6}).*.
Details
^ - start of string (implicit in matches())
(?!.*\d{6}) - a negative lookahead that fails the match if there are 0+ chars as many as possible followed with 6 consecutive digits
.* - any 0+ chars as many as possible
In Java, you may use it in the following way:
Boolean found = s.matches("(?s)(?!.*\\d{6}).*");
Note you may just try to find 6 digits with Matcher#find and if not found, proceed with the code excecution:
if (!Pattern.compile("\\d{6}").matcher(s).find()) {
// Cool, proceed
}

You can replace first group which match \d{6,} then check the result length with original length :
String text = "1234567 teststring";
boolean check = text.replaceFirst("\\d{6,}", "").length() == text.length();

What about this regex:
^(\D*)\d{0,5}(\D*)$

Related

Find a three-digit number in a string using replaceAll()

I have String from which I need to extract a keyword.
Something like: "I have 100 friends and 1 evil".
I need to extract "100" from that String using only replaceAll function and appropriate regex.
I tried to do it in that way:
String input = "I have 100 friends and 1 evil";
String result = input.replaceAll("[^\\d{3}]", "")
But it doesn't work. Any help would be appreciated.
You can consider any of the solutions below:
String result = input.replaceFirst(".*?(\\d{3}).*", "$1");
String result = input.replaceFirst(".*?(?<!\\d)(\\d{3})(?!\\d).*", "$1");
String result = input.replaceFirst(".*?\\b(\\d{3})\\b.*", "$1");
String result = input.replaceFirst(".*?(?<!\\S)(\\d{3})(?!\\S).*", "$1");
See the regex demo. NOTE you may use replaceAll here, too, but it makes little sense as the replacement must occur only once in this case.
Here,
.*? - matches any zero or more chars other than line break chars, as few as possible
(\d{3}) - captures into Group 1 any three digits
.* - matches any zero or more chars other than line break chars, as many as possible.
The (?<!\d) / (?!\d) lookarounds are digit boundaries, there is no match if the sequence is four or more digits. \b are word boundaries, there will be no match of the three digits are glued to a letter, digit or underscore. (?<!\S) / (?!\S) lookarounds are whitespace boundaries, there must be a space or start of string before the match and either a space or end of string after.
The replacement is $1, the value of Group 1.
See the Java demo:
String input = "I have 100 friends and 1 evil";
System.out.println(input.replaceFirst(".*?(\\d{3}).*", "$1"));
System.out.println(input.replaceFirst(".*?(?<!\\d)(\\d{3})(?!\\d).*", "$1"));
System.out.println(input.replaceFirst(".*?\\b(\\d{3})\\b.*", "$1"));
System.out.println(input.replaceFirst(".*?(?<!\\S)(\\d{3})(?!\\S).*", "$1"));
All output 100.

Java int to fraction

How can i change 4 -1/4 -5 to 4/1 -1/4 -5/1 using regex?
String str = "4 -1/4 -5";
String regex = "(-?\\d+/\\d+)";
Matcher matcher = Pattern.compile(regex).matcher(str);
My code finding only fraction but i want to find integer without fraction.
String result = str.replaceAll("(?<!/)\\b\\d+\\b(?!/)", "$0/1");
looks for entire numbers (\b\d+\b), not preceded by ((?<!/)) nor followed by a slash ((?!/)), and adds /1 to them.
Try (?<=-| |^)(\d+)(?!\d*\/)
Explanation:
(?<=...) - positive lookahead, assert, what precedes matches pattern inside
-| |^ - match either -, , or beginning of a line ^
(\d+) - match one or more digits and store in first capturing group
(?!\d*\/) - negative lookahead, assert what follows is not zero or mroe digits followed by \/.
Replace it with \1/1, so first capturing group followed by /1
Demo
I'm not sure I understand what you want to do here, but if you want to remove the slashes you can use:
str.replaceAll("\\/", " ");
This will leave you with a string having only the integers.

A regular expression to match and exclude word/string

I have strings like
patric NY abc other
patric ny
Expected output: patric ny and patric NY.
So, patric ny is varying statement which could be address. And abc MIGHT be there.
So, I want to retrieve random address like whatever we have before ABC
and if ABC is not present , give the complete string.
I tried
(.+?(?=abc))
It gives me result for patric NY abc other but not for patric ny.
Any help would be gratefully appreciated.
Extracting approach
You may use
^(.*?)(?:\s+abc\b.*)?$
See the regex demo.
Details
^ - start of string
(.*?) - Capturing group 1: any 0+ chars other than line break chars, as few as possible
(?:\s+abc\b.*)? - an optional non-capturing group that matches 1+ whitespaces, abc, a word boundary and any 0+ chars other than line break chars, as many as possible
$ - end of string.
Replacing approach
You may just remove 1+ whitespaces, abc and the rest from your string:
String result = input.replaceFirst("(?s)\\s+abc.*", "");
Or, if abc is a whole word:
String result = input.replaceFirst("(?s)\\s+abc\\b.*", "");
See the regex demo.
The replaceFirst() matches the first occurrence of the pattern and removes it.
Pattern details
(?s) - DOTALL flag making . match any char
\s+ - 1+ whitespaces
abc - an abc substring
\b - a word boundary
.* - the rest of the string
you can try this:
intput.replaceFirst("(patric (?:NY|ny)) ((?:abc|ABC).*)","$1")

Regular expression to determine if the String consists of more than 4 numbers

I want to extract URL strings from a log which looks like below:
<13>Mar 27 11:22:38 144.0.116.31 AgentDevice=WindowsDNS AgentLogFile=DNS.log PluginVersion=X.X.X.X Date=3/27/2019 Time=11:22:34 AM Thread ID=11BC Context=PACKET Message= Internal packet identifier=0000007A4843E100 UDP/TCP indicator=UDP Send/Receive indicator=Snd Remote IP=X.X.X.X Xid (hex)=9b01 Query/Response=R Opcode=Q Flags (hex)=8081 Flags (char codes)=DR ResponseCode=NOERROR Question Type=A Question Name=outlook.office365.com
I am looking to extract Name text which contains more that 5 digits.
A possible way suggested is (\d.*?){5,} but does not seem to work, kindly suggest another way get the field.
Example of string match:
outlook12.office345.com
outlook.office12345.com
You can look for the following expression:
Name=([^ ]*\d{5,}[^ ]*)
Explanation:
Name= look for anything that starts with "Name=", than capture if:
[^ ]* any number of characters which is not a space
\d{5,} then 5 digits in a row
[^ ]* then again, all digits up to a white space
This regular expression:
(?<=Name=).*\d{5,}.*?(?=\s|$)
would extract strings like outlook.office365666.com (with 5 or more consecutive digits) from your example input.
Demo: https://regex101.com/r/YQ5l2w/1
Try this pattern: (?=\b.*(?:\d[^\d\s]*){5,})\S*
Explanation:
(?=...) - positive lookahead, assures that pattern inside it is matched somewhere ahead :)
\b - word boundary
(?:...) - non-capturing group
\d[^\d\s]* - match digit \d, then match zero or more of any characters other than whitespace \s or digit \d
{5,} - match preceeding pattern 5 or more times
\S* - match zero or more of any characters other than space to match the string if assertion is true, but I think you just need assertion :)
Demo
If you want only consecutive numbers use simplified pattern (?=\b.*\d{5,})\S*.
Another demo
Of course, you have to add positive lookbehind: (?<=Name=) to assert that you have Name= string preceeding
Try this regex
([a-z0-9]{5,}.[a-z0-9]{5,})+.com
https://regex101.com/r/OzsChv/3
It Groups,
outlook.office365.com
outlook12.office345.com
also all url strings

How to work with regex to check a content of String

i need to check if a string have in your content minimum two commas and maximum three commas and one hyphen. I'm trying to make a regex to validate this String.
Ex:
String address = "Av. Rocio, 45, - Center";
String regex = "//,{2,3}|-{1}";
boolean isValid = address.matches(regex);
But don't working, always return false, what i did wrong? Thanks!
To match a string that has ONLY 2 or 3 commas and not more than 1 hyphen, use:
String regex = "(?s)^(?=([^,]*,){2,3}[^,]*$)(?=[^-]*-[^-]*$).*";
The matches method requires a full string match, thus, we need to add .*.
Note that {1} limiting quantifier is redundant, as - will match exactly 1 hyphen.
See IDEONE demo.
The regex (where . matches a newline due to (?s) inline dotall modifier) matches:
^ - start of string
(?=([^,]*,){2,3}[^,]*$) - Lookahead that checks the presence of 2 or 3 commas
(?=[^-]*-[^-]*$) - lookahead that requires only 1 hyphen to be in the string
.* - match all the string if the 2 conditions above are satisfied.

Categories

Resources