Regex: matching "S**E**", where * is a single integer value? - regex

I've read various resources and can't find anything which can easily explain how to match this format of string.
Something similar to this below?
/S[0-15][0-15]E[0-24]/

Regex [] matches all single characters in the brackets. You need to split it into multiple part expressions.
Try this:
/S(0?[0-9]|1[0-5])E([01]?[0-9]|2[0-4])/
About the use of the hyphen, pleas read this.

Related

How to combine three different text patterns For Word Find Wildcards

I'm new to the concept of REGEX and couldn't figure out how to use it properly in my VBA code.
I want to extract all strings that have the following formats:
AMT.xxx.xx.xxxxxx
AMT.xxx.xx.xxxxx
AMT.xxx.xxxxxx
AMT.xxx.xx.xxx.xxx
where Xs are numbers.
I tried this line and couldn't get the last two patterns.
With findRange.Find
.Text = "AMT.[0-9]{3}.[0-9]{2,}.[0-9]{5,}"
I tried to include the last two patterns using the OR(|) operator but it seems like it's not working.
With findRange.Find
"AMT.[0-9]{3}.[0-9]{2,}.[0-9]{5,} | AMT.[0-9]{3}.[0-9]{6}
| AMT.[0-9]{3}.[0-9]{2}.[0-9]{3}.[0-9]{3}"
How do I update my code to include all four patterns?
Thanks.
If you really must use Word Find with wildcard matching, Try this pattern:
AMT\.[0-9]{3}\.[0-9]{2}[0-9\.]{1}[.0-9]{3,7}
It should match the above 4 format but could potentially catch some other format due to its inability to match the later part of the string using zero or more occurrences.
Reference - https://wordmvp.com/FAQs/General/UsingWildcards.htm
Try this Regex
Matches all the cases that you've mentioned in your question
AMT\.\d{3}\.(?:\d{2,}\.?)+
Regex101 Demo
Tell me if its not working...
It can probably factor down to this
AMT\.\d{3}\.\d{2}(?:\.\d{3}(?:\d{2,3}|\.\d{3})|\d{4})
https://regex101.com/r/WtEvjf/1

Regex for extracting each word between hyphens

I am learning regex and trying to write a pattern that exactly matches each of the strings without'-' so that I can iterate for each of the groups and print the respective strings.
I have a string that looks like "Abcd001-wd2s-vwe1-20180e3103.txt"
I was able to write a regex for extracting Abcd001, wd2s and .txt from above text as shown below
(\A[^-]+)=> Abcd001
(-[^-]+-)=> wd2s
(\..*)=>.txt
However, I was unable to come up with the correct pattern for extracting the exact strings vwe1 and 20180e3103
It will be really helpful if you can guide me on this or if there is a better approach to achieve this?
Please note: [^-.]+ may give me all the words separately but I am looking for an option where I have a group defined for each of these strings so that its one to one mapping.
Thanks!
To get vwe1 or 20180e3103 from the example data, you might use a quantifier {2} or {3} to repeat matching one or more word charcters followed by a hyphen (?:\w+-){2}.
Then you could capture in a group ([^-.]+) matching not a hyphen or a dot.
(?:\w+-){2}([^-.]+)
Try the below regex
/\-([^\)]+)\-/gmi;
Also check the similar implementation:
https://stackoverflow.com/a/50336050/8179245

Regular expression dilemma

I'm trying for a few hours to write a pattern for some matching algorithm and I can't manage to find something for the following issue: given the example "my_name_is", I need to extract all words individually, as well as the whole expression. Consider that it may be a list of n examples, some that can be matched, some that cannot be matched.
"my_name_is" => ["my", "name", "is", "my_name_is"]
How can I do this, how should the regexp look like? Looking forward for your answers, thank you!
Regular Expressions are patterns used to match a string of characters. We usually use them to validate a string of characters, or to find and replace a specific pattern within text.
Here, it seems the outcome you're looking for is an array of strings that have been split using an underscore. Regex isn't what you're looking for.
Implementation would change based on language, but consider the following code:
function stringToArray(myStr)
{
words = str_split(myStr, '_');
return array_merge(words, [myStr]);
}
use re.findall with the following as your regex:
([^_]+)+?
This should match all sets of consecutive characters that don't contain the underscore.
As for the whole thing? You already have it, so there's no reason to regex the whole string

Regex Match between brackets (...)

I'm trying to grab 2 items from a simple line.
[Title](Description)
EDIT: actually a url looking to display called it description because i want it displayed not actually parsed.
[Trivium](https://www.youtube.com/user/trivium)
Grabbing between the brackets (...) doesn't seem to work at all for me. I've googled and found several variations with no luck, Thanks in advance :)
EDIT:
Tried the following:
[(.+?)]\((.*)\)
[(.+?)]\([^\(\r\n]*\)
[(.+?)]((.+?))
and a cpl more I cant find again
The first regex you listed almost has it right. Try using this regex instead:
\[.+?\]\((.*)\)
As #PM 77-1 pointed out, you need to escape the brackets by placing a backslash in front of them. The reason for this is that brackets are special regex metacharacters, or characters which have a special meaning. Brackets tell the regex engine to look for classes of characters contained inside of it.
Your original regex [(.+?)]\((.*)\) is actually doing this:
[(.+?)] match a period '.' 1 or more times
\((.*)\) match (anything), i.e. anything contained in parentheses
So this regex would match .....(stuff) but would not match [Title](Description), the latter which is what you really want.
Here is a link where you can test out the working regex:
Regex 101

Regular Expression to List accepted words

I need a regular expression to list accepted Version Numbers. ie. Say I wanted to accept "V1.00" and "V1.02". I've tried this "(V1.00)|(V1.01)" which almost works but then if I input "V1.002" (Which is likely due to the weird version numbers I am working with) I still get a match. I need to match the exact strings.
Can anyone help?
The reason you're getting a match on "V1.002" is because it is seeing the substring "V1.00", which is part of your regex. You need to specify that there is nothing more to match. So, you could do this:
^(V1\.00|V1\.01)$
A more compact way of getting the same result would be:
^(V1\.0[01])$
Do this:
^(V1\.00|V1\.01)$
(. needs to be escaped, ^ means must be on the beginning of the text and $ must be on the end of the text)
I would use the '^' and '$' to mark the beginning and end of the string, like this:
^(V1\.00|V1\.01)$
That way the entire string must match the regex.