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)….

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: tJohn</…

Find passwords values in JSON objects using Regex

I have a big JSON object which contains a lot of different JSON, most of them have the structure below (key: sometext-v1.password, and value: password for example: “apps”:[ “…

How could I select a range of characters up until a third / is present and select all if there is no third / using regular expression

I need to select a range within a string up until the third / is present. I currently have ^([^/]*/[^/]*/[^/]*/ but this will only work when there is more than 3 / in the string. Ideally I’d …

How can I get a special part in a string in Java? [closed]

I have to read commands from a file I have and take necessary actions according to each command. I was able to find out the process of reading from the file and what the command is, but I am having …

Java 8 convert for loop and summing of values to stream/lambda?

I have some String inputs that I am looping over that I am trying to convert to java 8 stream/lambdas but was having some issues. My boilerplate code looks like this: public static int count(List<…

Java regular expression to match valid Java identifiers

I need to create a regular expression able to find and get valid identifiers in Java code like this: int a, b, c; float d, e; a = b = 5; c = 6; if ( a > b) { c = a – b; e = d – 2.0; } else { d = e +…

How to put YML value inside the @Pattern(regexp = “HELLO|WORLD”)

I want to move the “HELLO|WORLD” value to the YML file. Then call the value from YML file inside the regexp. For example, Following is the YAML file YML FILE valid-caller-value: HELLO|WORLD …

Allow only left aligned zeros using regex

I am fairly new to using regex. I have a serial number which can take the following forms: VV-XXXXXX-P or VVXXXXXXP If the hyphen variant is used, then the number of ‘X’ can be variable. For example …

Java Regex vs. PHP, Dangling meta character ‘?’

I’m tagging this with PHP even though it’s a Java question. The regex is copied from a PHP source so I’m hoping some PHPers can help with the question. I decided to build a simple spam filter, just …