I have come up with a regex pattern to match a part of a Json value. But only PRCE engine is supporting this. I want to know the Java equalent of this regex.
Simplified version
cif:\K.*(?=(.+?){4})
Matches part of the value, leaving the last 4 characters.
cif:test1234
Matched value will be test
https://regex101.com/r/xV4ZNa/1
Note: I can only define the regex and the replace text. I don't have access to the Java code since it's handle by a propriotery log masking framework.
You can write simplify the pattern to:
(?<=cif:).*(?=....)
Explanation
(?<=cif:) Positive lookbehind, assert cif: to the left
.* Match 0+ times any character without newlines
(?=....) Positive lookahead, assert 4 characters (which can include spaces)
See a regex demo.
If you don't want to match empty strings, then you can use .+ instead
(?<=cif:).+(?=....)
You can use a lookbehind assertion instead:
(?<=cif:).*(?=(.+?){4})
Demo: https://regex101.com/r/xV4ZNa/3
Related
I am using the regex below to match strings; I was expecting the following results
Regex ^.*(?<!abc)(?<!def)(?<!ghi).xyz.co.*
Not Match
ghi.xyz.org
ghi-hipqr.xyz.org
abc-hipqr.xyz.org
Match
qrs.xyz.org
qrs-hipqr.xyz.org
However, ghi-hipqr.xyz.org is matching the regex (it shouldn't have since there is a look behind for the string ghi which is present in the string.
How can I fix it?
It is failing because ghi is not immediately before .xyz. in your string. Java (like many more regex engines) doesn't support variable length negative length look-behind assertion.
You can use this negative lookahead expressions instead:
^(?!.*\b(?:abc|def|ghi)\b).*\.xyz\.org.*$
RegEx Demo
(Edit, I said 'digit', I should have said 'alphanumeric char')
How do I extract a postfix from a string from a list of possibles (,X,,Y,,X),),Y). All need to be preceded by a alphanumeric character to be valid but the character is not to be extracted:-
What I am using is \w(,X|,Y|,X\)|\),Y){1}$ but this includes the preceding character (\w) in the extracted value.
(Unit tests pass but it's not sophisticated enough to test the returned match)
https://regex101.com/r/4Ggu7z/5/tests
Converting my comment to an answer.
You can use a negative lookahead instead of matching character in your regex. Here is working regex:
(?<=\w)(,[XY]|,X\)|\),Y)$
RegEx Demo
I have statement: <%=anything%><%=anything%>
and a regular expression: <%=\\s*(\\S+)\\s*%>.
The regex matches the stament as 1 match instead of 2 matches.
Can someone fix my regex?
Btw I use Java for my application
You are currently matching it all into one match, because regex usually is greedy, thus taking everything it can match into the match - so =anything%><%=anything is all matched by \S+. You could use the lazy modifier for the \S, so it matches as small as it has to, like so: <%=\\s*(\\S+?)\\s*%>. But there is an even better way to work with - as you don't want to match the closing >, just include it into a negative character class: <%=\\s*([^\\s>]+)\\s*%>
Here is a demo of it: https://regex101.com/r/bA4qY9/1
Note that you might have to double the backslashes again after testing in regex101
If you want to read further into it, have a look at http://www.regular-expressions.info/repeat.html
I need to replace all non-digit charaters in the string. For instance:
String: 987sdf09870987=-0\\\`42
Replaced: 987**sdf**09870987**=-**0**\\\`**42
That's all non-digit char-sequence wrapped into ** charaters. How can I do that with String::replaceAll()?
(?![0-9]+$).*
the regex doesn't match what I want. How can I do that?
(\\D+)
You can use this and replace by **$1**.See demo.
https://regex101.com/r/fM9lY3/2
You can use a negated character class for a non-digit and use the 0th group back-reference to avoid overhead with capturing groups (it is minimal here, but still is):
String x = "987sdf09870987=-0\\\\\\`42";
x = x.replaceAll("[^0-9]+", "**$0**");
System.out.println(x);
See demo on IDEONE. Output: 987**sdf**09870987**=-**0**\\\`**42.
Also, in Java regex, character classes look neater than multiple escape symbols, that is why I prefer this [^0-9]+ pattern meaning match 1 or more (+) symbols other than (because of ^) digits from 0 to 9 ([0-9]).
A couple of words about your (?![0-9]+$).* regex. It consists of a negative lookahead (?![0-9]+$) that checks if from the current position onward there are no digits only (if there are only digits up to the end of string, the match fails), and .* matching any characters but a newline. You can see example of what it is doing here. I do not think it can help you since you need to actually match non-numbers, not just check if digits are absent.
I want to do validation for a String which can only contains alphanumeric and only one special character. I tried with (\\W).{1,1}(\\w+).
But it is true only when I start with a special character. But I can have one special character at any place in String.
Use the ^ and $ anchors to instruct the regex engine to start matching from the beginning of the string and stop matching at the end of the string, so taking your regex:
^(\\W).{1,1}(\\w+)$
Please take a look at this Oracle (Java) tutorial on regular expressions.
Try this regexp: \w*\W?\w* (Java string: "\\w*\\W?\\w*")
This expression has a drawback of matching zero-length strings. If your input must have exactly one special character, remove the question mark ? from the expression.
use matcher.find() and not matcher.match() and search for \\w and remove plus (+) because it will match all alphanumeric characters sequence in your string.If your string contains only them, your regex will match whole string.
if I understand your regex correctly, this could solve your problem:
([\w]+)([^\w])([\w]+)