Regex nothing or some options - regex

I am trying to develop a regular expression to extract this: PT~MM:SS~EQP>G-G<EQP from a file.
PT is optional but if it is present it's only valid if it is 1P, 2P, 1EP or 2EP.
So if the example is: 3EP~101:37~POR>4-2<ISL it shouldn't be matching nothing but I am getting 2EP~101:37~POR>4-2<ISL as a match.
So far I've tried this:
(((1|2)P|(1|2)EP)~)?(0{0,1}([0-9]|[1-8][0-9]|9[0-9]|1[01][0-9]|120)):(0*([0-9]|[1-4][0-9]|5[0-9]))~[A-Z]{3}>[0-9]-[0-9]<[A-Z]{3}
Can someone help me?

This might what you are looking for ^(?:[12]E?P)?~?\w+?:\w+?~\w+?>\w-\w<\w{3} (https://regex101.com/r/T8Cy4C/6). Although you did not specified fully what are the requirements for each parts.

Related

Regular Expressions for finding files

Ok I'm giving up and ask the question after I read through the help article of regex and still don't have a clue what I'm looking for:
I Have a list of files:
files <- c("files_combined.csv","file_1-10.csv","file_11-20.csv",
"file_21-30.csv","file_2731-2740.csv","file_2731-2740.txt")
I want only the csv files that start with "file_" and end with ".csv". I know the it looks something like this:
grep(pattern = "^file_???.csv$" ,files)
But I need to find the correct regular expression that ignores the number of characters between the first and the second pattern ("file_" + ".csv"). I'd really appreciate if somebody knows a complete list with the regular expressions in R since it is tedious to read through the help every time and, as in my case not successful, sometimes...
R offers a function for doing wildcard expansion using glob patterns for those who don't like regex:
files <- Sys.glob("file_*.csv")
This should match your pattern.
Thanks a lot! Seems David Arenburg and Heroka, you came up with the solution at the same time. Also thanks to MichaelChirico for providing the cheatsheet.
This is the answer to my specific problem:
grep("^file_.+\\.csv$",files,ignore.case = T)
As for problems with regex, this is helpful as well txt2re

regular expression for string with dot del

I am looking for a regular expression for the following string:
SQL Err 100 on \TEST1.$PROD01.TEST.XYZ562
I want to search for anything with TEST*.$PROD*.TEST.XYZ*.
Can anyone help with the regular expression?
Basic effort was required, you could just tried it yourself and, I believe, find an answer on your own! For future needs, try site regex101.com, it helps to work with regulars.
So, you need to find a string containing TEST, then something, then . then $PROD value, again anything, and .TEST.XYZ, and finally anything, where $PROD is, as I understand, a variable without special characters (as [](){}.^\).
So, you get that:
TEST[^.]*\.<$PROD value should go here>[^.]*\.TEST\.XYZ
That should be enough. You will need to supstitude variable $PROD on your own, but I don't know your language, so I can't help with that part. Maybe something like this:
"TEST[^.]*\." + $PROD + "[^.]*\.TEST\.XYZ"

Regular expression to extract argument in a LaTeX command

I have a large LaTeX document where I have defined a macro like this:
\newcommand{\abs}[1]{\left|#1\right|}
I want to get rid of it by replacing in all the document \abs{...} by \left|...\right|, so I am thinking in a regular expression. I am familiar with their basics but I do not know how to find the bracket that closes the expression, given that the following situations are possible:
\abs{(2+x)^3}
\abs{\frac{2}{3}}
\abs{\frac{2}{\sin(2\abs{x})}}
What I have been able to do for the moment is \\abs\{([^\}]*)\} and then replace as \left\1\right|but it is only able to deal with the pieces of code of the first kind only.
By the way, I am using the TeXstudio regular expression engine.
Well, I did a little more of research and I managed to solve it. According to this response in a similar question, it suffices to use recursive regular expressions and a text editor that supports them, for example Sublime Text 2 (I could not do it with TeXstudio). This does the trick:
Find: \\abs\{(([^\{\}]|(?R))*)\}
Replace: \\left|\1\\right|
EDIT 1: Actually this solves only the two first cases, but fails with the third, so any idea on how to improve the regular expression would be appreciated.
EDIT 2: See comment from #CasimiretHippolyte for full answer using \\abs\{((?>[^{}]+|\{(?1)\})*)\}

Regular Expression for recognizing files with following patterns az.4.0.0.119.tgz

I am trying to find a regular expression that will recognize files with the pattern as az.4.0.0.119.tgz. I have tried the regular expression below:
([a-z]+)[.][0-9]{0,3}[.][0-9]{0,3}[.][0-9]{0,3}[.]tgz
But, no luck. Can anyone please point me in the right direction.
Regards,
Shreyas
Better to use a simple regex like this:
^([a-z]+)\.(?:[0-9]+\.)+tgz$
You just forgot one number part:
([a-z]+)[.][0-9]{0,3}[.][0-9]{0,3}[.][0-9]{0,3}[.][0-9]{0,3}[.]tgz
or
([a-z]+)[.]([0-9]{0,3}[.]){4}tgz
Depending on where and how you use the regex, you might want to surround it in ^...$.
Your pattern has 4 chiffers group, your regexp only 3.

Regular Expression with specific criteria

Hey everyone, I'm trying to type a regular expression that follows the following format:
someone#somewhere.com or some.one#some.where.com
There are no special characters or numbers permitted for this criteria. I thought I had it down, but I'm a bit rusty with regular expressions and when I tested mine, it failed all across the boards. So far, my regular is expression is:
^[a-zA-Z]+/.?[a-zA-Z]*#[a-zA-Z]+/.?[a-zA-Z]*/.com$
If anyone could help me, it would greatly be appreciated, thanks.
your regex looks good. I think you need to change the / to \ in front of the . .
Additionally, if you don't want someone.#somewhere..com pass your regex, u should change your regex to
^[a-zA-Z]+(\.[a-zA-Z]+)?#[a-zA-Z]+(\.[a-zA-Z]+)?\.com$
(not completely sure about the brackets () though, but i think that should be working)
its a backslash to espace dots. Also put the the parenthesis around the . and what follows otherwise an email like abc.#cde..com would be valid.
^[a-zA-Z]+(\.[a-zA-Z]+)?#[a-zA-Z]+(\.[a-zA-Z]+)?\.com$
It looks mostly OK. Change your / to \ though...
For the second case, I would ensure that if you have a . in the middle, it must be followed by more letters:
^[a-zA-Z]+(\.[a-zA-Z]+)?#[a-zA-Z]+(\.[a-zA-Z]+)?\.com$