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

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$

Related

How to find and replace space between digits in a string column?

I need to find and replace any space between digits in a long string using regular expression.
I have tried to use regular expression such as [0-9][\s][0-9] and then regexp_replace such as .withColumn('free_text', regexp_replace('free_text', '[0-9][\s][0-9]', '')).
However, the regex matches 1(space)4 where I would like to have only (space)
Here is an example:
What I have:
"Hello. I am Marie. My number is 768 990"
What I would like to have:
"Hello. I am Marie. My number is 768990"
Thanks,
Here is one way to do this, using capture groups:
.withColumn('free_text', regexp_replace('free_text', '([0-9])\s([0-9])', '$1$2'))
The idea here is to match and capture the two digits separated by a whitespace character in between them. Then, we can replace by just the two digits adjacent.
Your pattern matches a digit, whitespace character and a digit. Note that \s also matches a newline.
If supported, you could use lookarounds instead of matching the digits:
(?<=[0-9])\s(?=[0-9])
.withColumn('free_text', regexp_replace('free_text', '(?<=[0-9])\s(?=[0-9])', ''))

Match everything before last dot 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

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".

Perl matching characters bigger than a given length

I have been struggle to write regex that matches words longer than a given length within parentheses. First I thought I could do this with \(\w{a,}\) but I realize that it doesn't match with words with white space (ab cd ef). All I want to do is find out any characters within parentheses longer than, for instance, 3 characters. How can I resolve this problem ?
What is a word with white space?
if you want to match any character then use .
\(.{3,}\)
. matches any character except newlines
But be careful, this is greedy. it will match for example also
(a)123(b)
To avoid this you could do something like
\([^)]{3,}\)
See it here online on Regexr
[^)] means any character except a )
You could use a character class that includes both \w and \s:
\([\w\s]{a,}\)
Maybe do you mean?
\([\w\s]{a,}\)
if it has a space in it it's not a word anymore.
is matching any characters fine \(.{a,}\)? Or you just need the whitespace \(\(\w|\s\){a,}\)?

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)/