Regex matching groups by prefix - regex

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

Related

Regular Expression Should not start with a character and contain a sequence

For example, should not start with h and should contain ap.
Should match apology, rap god, trap but not match happy.
I tried
^[^h](ap)*
but it doesn't match sequences which start with ap like apology.
You may use
^(?!h).*ap
See the following demo. To match the whole string to the end, append .* at the end:
^(?!h).*ap.*
If you plan to only match words following the rules you outlined, you may use
\b(?!h)\w*ap\w*
Or, without a lookahead:
\b([^\Wh]\w*)?ap\w*
See this regex demo and the demo without a lookahead.
#WiktorStribiżew's comment with negative lookahead is correct (you might want to add .* to it if you want to match the whole string).
For completeness, you can also use alternation:
^(?:[^h].*ap|ap).*
Demo: https://regex101.com/r/ecVTGm/1

how to match statement that combine word,/,digits like /dev/home/hello-world

i want to match following statement in regex:
/
/dev/shm
/var/lib/snap/hello-world/27
any idea?
and this is my last try:
\/((\w+)|(\/\w+-\w+))+
but it seems that, dash-statements treat like a seperate part.
You can use this regex using a negated character class:
^\/(?:[^/]*\/)*.*$
RegEx Demo
RegEx Breakup:
^: Start
\/: Match literal /
(?:[^/]*\/)*: Match 0 or more groups of non-slash strings followed by a /
.*: Match anything after last/`
$: End
If you are a positive person and don't want to use negative classes (as proposed by #anubhava), you can check this fully positive regex:
^(?:(?:\/)|(?:\/\w+)|(?:\/\w+-\w+))+$
RegEx Demo
thanks everyone. all your answers works. but i find a simple approach:
(\/([\w\d\-\/])*)
RegEx Demo

Match Latin words which not in the hook

I'm trying to filter words which is not in the "[ ]".
Why is this not working?
[^\[][\u0000-\u024F]+[^\]]
The reason your expression is not working is that it matches all text inside brackets as well as outside.
This is the best I've been able to do:
/(?:^|])[^[]+/g
It includes the ]s in the match because look-behind is not allowed:
http://regexr.com/3c515
If look-behind were allowed, this would be the ticket:
/(?:^|(?<=]))[^[]+/g
https://regex101.com/r/lK9tS7/3
Because this will match [\u0000-\u024F]+ and 2 character which will be matches by [^\[]. If you want to your regex engine match the whole of pattern you need to use start and end anchors in your regex :
/^[^\[][\u0000-\u024F]+[^\]]$/m
But this will work if your string is contain words in each line, which is not a proper way.
As a better way you can use negative look arounds :
(?<!\[)[\u0000-\u024F]+(?!\])

match a word with out consecutive characters in it

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

Regex to not have a sequence of repeated characters

I have a regex /(.)\1{1,2}/ that matches the text that has sequence of repeated characters.
But I want a regex that does the opposite. I don't want to negate it. How do can I do that?
(?!(.)\1{1,2}).
or you can try
(.)(?!\1)
You can try this.This uses a negative lookahead.See demo.
http://regex101.com/r/hQ1rP0/8
It couldn't be possible without negation,
(.)(?:(?!\1).){1,2}
DEMO
The below regex would capture the first character and checks for more than two repeated characters. If there are more than two repeated characters at the start, it won't match that string.
^(.)\1(?:(?!\1).)+$
DEMO
You can try this
"^(?!.*(.)\1).{0,11}$"