val pattern = “[A-Z]{2,3}[0-9]{4}”.r val extractedData = pattern.findFirstIn(“find ABCD1234”) I have the above code to look for valid data. Input: find DTD0001 Expected Output: …
Tag: regex
Separate definitions of decimal number and word in ANTLR grammar
I’m working on defining a grammar in ANTLR4 which includes words and numbers separately. Numbers are described: and words are described: The simplified grammar below describes the addition between either a word or a letter (and needs to be defined recursively like this): The issue is that when I enter ‘d3’ into the parser, I get a returned instance of
How to split a string into two considering end of the string with numbers (which is inside the squre brackets)
I need to split strings into two with a given separator using regex. Sample Strings and expected output as below Each and every String should be split into two if it has numbers within the square bracket at the end of the input string. Can anyone tell me a regex to do this? Note: I find out the regex :
How to replace occurance of numbers with single value?
I have below code Which gives output: I need: So from string if numbers found just replace with %d one only, not every digit. Answer You need to change your regex to “[0-9]+” Oracle’s doc: Quantifiers
Why does this regex fails to check accurately?
I have the following regex method which does the matches in 3 stages for a given string. But for some reason the Regex fails to check some of the things. As per whatever knowledge I have gained by …
Make RegEx optional groups either BOTH be present and match, or if ONE is missing not match/fail (java)
I have a RegEx pattern I am using and it works (mostly), but there is one bug. I have 3 separate groups to capture the values, serverXXX (group 1), -Site (group 2, optional) and YY (group 3, optional). Below is the RegEx pattern definition: As you can see by the regex pattern, the server is followed by three digits, 0-9.
How to properly split on a non escaped delimiter?
I have the following example string: with | being the delimiter and being the escape character. A proper split should look as follows: Also I need this logic to be generally applicable in case the delimiter or the escape consists out of multiple characters. I already have a regex which splits at the correct position, but it does not
Cant find pattern with Scanner in Java 8
I’m working with Java 8 on eclipse and trying to use regex to detect strings in a file, but I’m not detecting anything. I’ve tested the regex on its own and it successfully match’s strings in the file …
Java regex. Keep special characters only when they’re surrounded by numbers
I’m making an word frequency program and I’m trying to process text to make it manageable. I’m trying to remove all special characters except $%^*+-=,./<> which are a part of a number. I have virtually no experience with regular expressions and after reading a bunch on it, I tried using the negative lookadead and negative lookaround to get something like
Java Pattern and Matcher no result
i’m writing this Java program for fun and i’m trying to get groups of substring with a regex. My string is read from a file and it’s something like: t<firstName type=”String”>John</firstName> where t is a tab. I wrote this code to isolate the needed substring, but while on online testing it works, in my code it says “no matches”. My