match a word with out consecutive characters in it - regex

I am new to java regex. Can you please give a matching pattern for the following requirement.
Example:
"Apple" or "Google" should not be matched since there are consecutive characters in them.
But, words like "Hard2Crack" or "Sometimes" should match.
Your help is appreciated.

You can use this regex:
^(?:(.)(?!\1))*$
Demo

(?!.*?([a-z])\1)^.*$
You can try this.uses a negative looakahead.
See demo.
http://regex101.com/r/oC3nN4/13

Related

Regex to match different characters at same position in string

Let's say I have the text a123456. I want a string of b123456 to match. So essentially, 'match if all characters are the same except for the first character'. Am I asking for the impossible with regex?
Use the dot (.) to match any character. So, a possible Regex would be:
/^.123456$/
If you want to use zero length assertion with regex, you can have lookbehind approach in following way :
(?<=\w)your_value$ // your_value should be text which you want to check
I think you can figure it out on your own. This ain't tough, just needs some understanding between you and Regex. Why don't you go through the following links and try to make a regex on your own.
https://www.talentcookie.com/2015/07/regular-expressions/
https://www.talentcookie.com/2015/07/lets-practice-regular-expression/
https://www.talentcookie.com/2016/01/some-useful-regular-expression-terminologies/

Regex matching groups by prefix

I'm having the following string: CL_6x CL_5c CL_234 CL_ERB14 1D CL_6y
I need to find a regex to extract groups like this
CL_6x
CL_5c
CL_234
CL_ERB14 1D
CL_6y
As you can see they're all prefixed with CL_
Any ideas how to achieve this?
You need to use a positive lookahead based regex.
\bCL_.*?(?=\s*CL_|$)
This should match until the next CL_ or end of the line.
DEMO
CL_.+?\b
Try this.See demo.\b is word boundary
https://regex101.com/r/uF4oY4/86
EDIT:
for test cases like CL_ERB14 1D.
use
CL_\S+(?:\s*(?!CL_)\S+)
See demo.
https://regex101.com/r/uF4oY4/87
You can use following regex.
^CL_.+\b
Explanation
^: Starts with
CL_: Matches literal CL_
.+: Matches any characters any number of times
\b: Word boundary

Regex to match not more then a specific number of upper case characters

How can I create a regular expression to check that there are not more then n capital letters in a string i.e.
if n=3 then
aAnnBccD#!
AAbbC
should match, while
AbCdeFgHiJ
should fail.
Please advise on the same.
Just to check, example with lookahead:
^(?!(?:.*?[A-Z]){4})
This fails at strings that contain {4} (more than 3) ...A-Z
see test at regex101
Just try with following regex:
^[^A-Z]*([A-Z][^A-Z]*){0,3}$
Something like
^([^A-Z\n]*[A-Z][^A-Z\n]*){0,3}$
Regex Demo

Match string not containg a certain phrase

I need to find all instances of the word "confidential" in a message except when it is used in the phrase "confidential and proprietary" in which case it is ok and I dont need to pick it up through regex.
Thanks all in advance!
-P
You can use negative lookaround (http://www.regular-expressions.info/lookaround.html)
This regex will match: (confidential) (?!and proprietary) if your engine support lookaround.
demo: http://regexr.com?36itq
Using word boundaries \b is also an option here.
\bconfidential\b(?! and proprietary\b)

Regex match string conditions

Since i'm not so good at regex how can i match some conditions in a string, StaticString_1number:1number:more than 1number.
Example:
string_3:0:12344555 - Match
string_s:0:12344555 - No match
string_3:s:12344555 - No match
string_3:0:123s4555 - No match
Thanks.
This Regex would solve your problem:
^[a-zA-Z]+_[\d]{1}:[\d]{1}:[\d]+$
You can check this link for verification: http://regexr.com?34uj5
If I understand your pattern StaticString_1number:1number:more than 1number correctly your regex to match against such strings could look like the following:
'^[a-zA-Z]+_[0-9]:[0-9]:[0-9]+$'
or if your environment support character classes:
'^\w+_\d:\d:\d+$'
This should suit your needs:
^[^_]+_\d:\d:\d+$
Demo
If the initial String can only have characters a-z then the following should work :
[a-z A-Z]+_\d:\d:\d+
this will match any number of letters up to an underscore then look for single digit before and after colon and multiple digits after second colon.
but you should really have an attempt your self. if in python you could try re-try or in javascript regexpal and try out your regex patterns there first.
This may help : ^[a-zA-Z]*_[0-9]:[0-9]:[0-9]*$