Matching any character in regex? - java

I have a state machine which is capable of matching the comments. So it can handle :
/* /* */ */
But I bogged down of skipping the contents that are inside the comment lines. Currently my comments-word regex looks something strange :
[0-9A-Za-zA-Z0-9\*\(\*\*\)\.\{\}\_\;\,\-\:" "\#]*
Are there any simple regex ( in java ) which matches all the characters? Alphabets along with special characters?
Thanks for the help.

use . (dot) if you want to match any character.
See here: Dot

. matches anything once. .* will match 0 or more of anything, while .+ will match one or more, depending on your needs.

. is the character that matches all other characters, with the possible exception of newlines (depending on whether DOTALL is enabled).
If you want to match everything EXCEPT a certain character or two, use [^...] syntax (such as [^0-9a-fA-F] to avoid matching every hexadecimal digit).
It is often useful to add a trailing ? to expressions with a dot, to match the fewest characters as possible (such as .*? or .+?). Otherwise, an unterminated dot expression may match the rest of the string.

Related

Regex Match Reset \K Equalent In Java

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

Regular Expression to Match character at a position and ignore characters after

Want to match the character at position 7 to either be - or an Uppercase letter
This is what I have ^.{6}[-(A-Z)]
Though this matches the first 7 characters, it doesn't match the whole string. Any help appreciated.
I am using Java and wanting .matches() to return true for this String
Though this matches the first 7 characters, it doesn't match the whole string.
That's the right explanation of what is going on. You can skip over the rest of the string by adding .* at the end. Additionally, the ^ anchor at the front of the expression is implied, so you can drop it for a pattern of
.{6}[A-Z-].*
As mentioned You can use .* to match anything after your specific character so use
^.{6}[-A-Z].*
and also no need of () if you don't want to capture that specific character

Regex - Optional Positive Lookbehind

I'm trying to match such that all the characters after the last / and before . gets matched. My current challenge is that . is only sometimes present.
I have an example here: https://regex101.com/r/ThWZwX/3
Where I'm hoping to match the 'match' text in both scenarios.
Thanks,
You can use negated character class in a capture group without any need of a lookahead:
.*\/([^.]*)
RegEx Demo
We use .*\/ to match last / by using a greedy match of .* and then we use negated character class [^.]* to match until we get a dot or everything if dot is not found.
Also note that we use ([^.]*) to capture this match.
No need for anything complex
/([^/.]*)(?!.*/)
https://regex101.com/r/KHyXcJ/1

Regex-How to prevent repeated special characters?

I don't have an experience on Regular Expressions. I need to a regular expression which doesn't allow to repeat of special characters (+-*/& etc.)
The string can contain digits, alphanumerics, and special characters.
This should be valid : abc,df
This should be invalid : abc-,df
i will be really appreciated if you can help me ! Thanks for advance.
Two solutions presented so far match a string that is not allowed.
But the tilte is How to prevent..., so I assume that the regex
should match the allowed string. It means that the regex should:
match the whole string if it does not contain 2
consecutive special characters,
not match otherwise.
You can achieve this putting together the following parts:
^ - start of string anchor,
(?!.*[...]{2}) - a negative lookahead for 2 consecutive special
characters (marked here as ...), in any place,
a regex matching the whole (non-empty) string,
$ - end of string anchor.
So the whole regex should be:
^(?!.*[!##$%^&*()\-_+={}[\]|\\;:'",<.>\/?]{2}).+$
Note that within a char class (between [ and ]) a backslash
escaping the following char should be placed before - (if in
the middle of the sequence), closing square bracket,
a backslash itself and / (regex terminator).
Or if you want to apply the regex to individual words (not the whole
string), then the regex should be:
\b(?!\S*[!##$%^&*()\-_+={}[\]|\\;:'",<.>\/?]{2})\S+
[\,\+\-\*\/\&]{2,} Add more characters in the square bracket if you want.
Demo https://regex101.com/r/CBrldL/2
Use the following regex to match the invalid string.
[^A-Za-z0-9]{2,}
[^\w!\s]{2,} This would be a shortest version to match any two consecutive special characters (ignoring space)
If you want to consider space, please use [^\w]{2,}

Java String validation only one alphanumeric with Regex

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]+)

Categories

Resources