Match everything before last dot regex - regex

I need a regex that will match everything before a last dot in my string. For example, I have text like this:
if_blk4.if_blk1.if_blk1
I would like to get the if_blk4.if_blk1.
Thanks!

To match everything up to (but not including) the last dot, use a look ahead for a dot:
.*(?=\.)
The greedy quantifier * makes the match include as of the input much as possible, while the look ahead (?=\.) requires the next character in the input to be a dot.

How about
regexp {.*(?=\.[^.]*$)} $text match
i.e. matching any characters that lead up to a (not matched) sequence of dot and zero or more characters that aren't dots, followed by the end of the string.
(The regular expression {.*(?=\.)} is equivalent as regular expression matching doesn't need to be anchored.)
or (faster)
file rootname $text
Documentation:
file,
regexp,
Syntax of Tcl regular expressions

Related

Regex - Finding fullstops (periods) that aren't followed by a space

I'm trying to create a simple Grammar correction tool.
I want to create a regular expression that finds fullstops (" . ") that are not followed by a space so I can replace that with a fullstop and space.
For e.g. This is a sentence.This is another sentence.
Only the first fullstop in the above example should be matched in the expression.
I've tried /\.[^\s]/g but it returns an additional character after the matched fullstop. I would like to match only the fullstop.
How can I do this?
The negated character class [^\s] in the pattern expects a match (any character except a whitespace character), that is why you have the additional character.
If you want to match the dot only, you could use a negative lookahead to assert what is on the right is not a whitspace char or the end of the string:
\.(?!\s|$)
Regex demo
To not match a dot that is not followed by a whitespace char excluding a newline:
\.(?![^\S\r\n])
Regex demo
You can look for all dots using:
(\.)
This will match all dots on below examples:
This is a sentence.This is another sentence.
i am looking. for dots. . ...
You can add a |$ to seek for end of line, and with a little tweak, you get a regex that match all dots not followed by whitespace nor being on the end of a line:
(\.(?!\ |$))
Note that there's a whitespace as literal here. The "must-work-everywhere" example will be like:
(\.(?![[:space:]]|$))
If not, search on the regex reference on the language you use.

Regular Expression to find matches of String series

I'm a new bee in regular expression and need help in delimiting string that follows a certain pattern.
My string will be always follow a pattern like ".(0.satQA).(1.SomewhatEnjoyable).(0.satQC).(0.ShorterThanExpected).(0.Q12).(0._1)".
My first search should return (the bold one here) (0.satQA).(1.SomewhatEnjoyable).(0.satQC).(0.ShorterThanExpected).(0.Q12).(0._1)
second as (0.satQA).(1.SomewhatEnjoyable).(0.satQC).(0.ShorterThanExpected).(0.Q12).(0._1)
Third as (0.satQA).(1.SomewhatEnjoyable).(0.satQC).(0.ShorterThanExpected).(0.Q12).(0._1)
In short, I need to delimit this into 3 parts (in this case). It should start with "(" and follow with characters (any), must include ").(" in the middle and then end with ")".
The regex for the pattern you are looking for is \(.*?\)\.\(.*?\)
.*? is a reluctant greedy quantifier, meaning that will match as it can before the next match in the regex
You also need to escape characters like . ) and (

regex string end with .log and contains chars numbers and -

Can someone tell me the regex pattern to match everything that ends with .log and contains chars, numbers and -
for example:
"syslog-12-10-2011.log"
You can try:
^[a-z0-9-]+\.log$
The regexp you're looking for is
^[A-Za-z0-9-]*\.log$
note that dot requires escaping and dash must be the first or last character inside square brackets (otherwise it denotes character range).
Note that this matches filename '.log'. Replace the star with a plus to have it match filenames with at least one character before the dot in '.log'.
This is a regex that you can use:
^[a-zA-Z0-9\-]+\.log$
With a case insensitive regular expression:
^[A-Z]+-([0-9]{2}-){2}[0-9]{4}\.log$
It's a bit more precise than what you asked (it matches text-nn-nn-nnnn.log, where n is a digit). If you are using POSIX regex (like in grep for instance), you will have to escape parenthesis and brackets:
[A-Z]+-\([0-9]\{2\}-\)\{2\}[0-9]\{4\}\.log$

regular expression no characters

I have this regular expression
([A-Z], )*
which should match something like
test, (with a space after the comma)
How to I change the regex expression so that if there are any characters after the space then it doesn't match.
For example if I had:
test, test
I'm looking to do something similar to
([A-Z], ~[A-Z])*
Cheers
Use the following regular expression:
^[A-Za-z]*, $
Explanation:
^ matches the start of the string.
[A-Za-z]* matches 0 or more letters (case-insensitive) -- replace * with + to require 1 or more letters.
, matches a comma followed by a space.
$ matches the end of the string, so if there's anything after the comma and space then the match will fail.
As has been mentioned, you should specify which language you're using when you ask a Regex question, since there are many different varieties that have their own idiosyncrasies.
^([A-Z]+, )?$
The difference between mine and Donut is that he will match , and fail for the empty string, mine will match the empty string and fail for ,. (and that his is more case-insensitive than mine. With mine you'll have to add case-insensitivity to the options of your regex function, but it's like your example)
I am not sure which regex engine/language you are using, but there is often something like a negative character groups [^a-z] meaning "everything other than a character".

Regular expression not matching specific string

My use case is as follows: I would like to find all occurrences of something similar to this /name.action, but where the last part is not .action eg:
name.actoin - should match
name.action - should not match
nameaction - should not match
I have this:
/\w+.\w*
to match two words separated by a dot, but I don't know how to add 'and do not match .action'.
Firstly, you need to escape your . character as that's taken as any character in Regex.
Secondly, you need to add in a Match if suffix is not present group - signified by the (?!) syntax.
You may also want to put a circumflex ^ to signify the start of a new line and change your * (any repetitions) to a + (one or more repititions).
^/\w+\.(?!action)\w+ is the finished Regex.
^\w+\.(?!action)\w*
You need to escape the dot character.
\w+\.(?!action).*
Note the trailing .* Not sure what you want to do after the action text.
See also Regular expression to match string not containing a word?
You'll need to use a zero-width negative lookahead assertion. This will let you look ahead in the string, and match based on the negation of a word.
So the regex you'd need (including the escaped . character) would look something like:
/name\.(?!action)/