How to regex for alphanumeric pattern with specific string - regex

Needle in haystack: Specific string followed by exact set of numbers
How can I search for ABC???? where ABC should be exactly that, but the ???? must be exactly four numbers, ideally followed by whitespace.
Illustrative examples:
LHRJFKABC1234 233 <-- Has needle
EABC123 LHRJFK <-- Does not have needle as only 3 numbers following ABC
Something tells me I need to search for string + something like (\d{4}) for the 4 numbers. But not sure quite how to puzzle it all together.
What I've found so far:
Regular expression to match standard 10 digit phone number
Regular Expression to match specific string followed by number?

For things like this I find an online checker like Rubular very handy.
Unless I'm misunderstanding, the regex ABC\d{4}\s should work for you. Do you need groupings (i.e. to match the 4-digit part)?
Try it out on Rubular here

Related

Regex: Can I name a pattern/group to condense code when reusing the pattern/group?

I want to name or reference a grouped pattern for reuse without enforcing a similar match across this pattern to shorten my regex.
I'm sure there are examples with more obvious benefit to doing something like this, but let's say I want to match something like a string composed of six 2-digit hex numbers separated by colons, such as 38:f8:b7:90:45:92.
The pattern I came up with for this is (?:[0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}
[0-9A-Fa-f]{2} is used to represent a 2-digit hex number, which I'll call the hex pattern. Instead of repeating this a second time after the colon, I'd like a way to name this or something so I can shorten my regex.
I've already tried (?:(?<x>[0-9A-Fa-f]{2}):){5}\k<x> but rather than being the original hex pattern I made, it seems to only match with the last match the hex pattern found. For example, running this regex on 38:f8:b7:90:45:92 will basically turn the pattern into ([0-9A-Fa-f]{2}):){5}45 since 45 is the last match the original hex pattern found.
Therefore only something like 00:18:12:c1:5a:5a, where the last two 2-digit numbers are the same, will match.
Is there a way to name a pattern for complete reuse?
If supported, you could make use of repeating the subpattern
That might look like:
(?:([0-9A-Fa-f]{2}):){5}(?1)
^ ^^^^
Regex demo
Or by name:
(?:(?<x>[0-9A-Fa-f]{2}):){5}(?&x)
^^^^^ ^^^^^
Regex demo

regular expression pattern not matching with my string

How can I rewrite below regular expression
/((?:\+|00)[17](?: |\-)?|(?:\+|00)[1-9]\d{0,2}(?: |\-)?|(?:\+|00)1\-\d{3}(?: |\-)?)?(0\d|\([0-9]{3}\)|[1-9]{0,3})(?:((?: |\-)[0-9]{2}){4}|((?:[0-9]{2}){4})|((?: |\-)[0-9]{3}(?: |\-)[0-9]{4})|([0-9]{7}))/g
for matching with this pattern
+1(555)532-3455
Your expression seems needlessly complicated for matching that string. You're trying to match the following aspects:
a plus sign with a country code
an area code wrapped in brackets
the first 3 digits of the phone number
a dash
the last 4 digits of the phone number
no spaces anywhere
To match this you only need
^\+[0-9]\([0-9]{3}\)[0-9]{3}\-[0-9]{4}$
If you need to allow for optional spaces between the country code, area code and phone number you can add them to the expression.
^\+[0-9]\s?\([0-9]{3}\)\s?[0-9]{3}\-[0-9]{4}$
(I may have unnecessary escapes \ in there)
Alternatively you can do:
^\+?\d(?:\s*(?:\(|\-)?\s*)\d{3}(?:\s*(?:\)|\-)?\s*)(?:\d{3}(?:\s*\-?\s*)?\d{4})$
See the regex demo for some examples of what this regex can and can not match.

Regular Expression allow only numbers, commas and dashes

I'm trying to come up with a Data Annotation regular expression to match the following formats.
34
38-30
100,25-30
4-5,5,1-5
Basically the expression should only allow numbers, -(dash) and ,(comma) in any order
I tried following but couldn't get it working.
[RegularExpression(#"(0-9 .&'-,]+)", ErrorMessage ="Lot numbers are invalid.")]
It's ^[0-9,-]*$. Check out this demo.
I think your use case is having a CSV list of numbers, or ranges of numbers (identified as a number followed by a dash followed by another number). We can use the following regex:
[0-9]+(?:-[0-9]+)?(,[0-9]+(?:-[0-9]+)?)*
This regex matches a number, followed by an optional dash and another number, that quantity then followed by comma and another similar term, any number of times.
In the demo below I added anchors on both sides of the regex. Whether you need to do this depends on how you plan to use the pattern.
Demo

Can't use regular expression to match exact string

Given a string below:
String s = "sschk##123456sschk##123456gme##100&200&300&1,2,3,4,5$6,7,8,9,0sschk##123456";
I apply a pattern, sschk##\\d+? or sschk##.+? want to get all sschk##123456 and replace them with an empty string. Please note that number after sschk## might different each time I got it, for example sschk##321321.
But I only got
[sschk##1, sschk##1, sschk##1]
What pattern should I apply to get exact each sschk##123456, so that I can do find and replace later.
Thanks a lot.
The problem with your regex was that you have used "?" marker which toggles the greediness of the "+" in your regex, so your regex "sschk##\d+?" means "a string sschk## followed by 1 or more numbers, but match as less digits as possible". Removing "?" would mean "a string sschk## followed by 1 or more numbers (match as much digits as possible)"
Your regex statement might look like this perhaps: sschk##\\d{6} and it would match a string "sschk##" followed by exactly 6 digits. If you want to match the string "sschk##" followed with variable length of digits, but not more than 6, you might use sschk##\\d{1,6}. If you need to match any number of digits after the string "sschk##" then use sschk##\\d+
I think I got it done.
Just apply the pattern like this
(sschk##\\d+)

Regular Expression for matching a phone number

I need a regular expression to match phone numbers. I just want to know if the number is probably a phone number and it could be any phone format, US or international. So I developed a strategy to determine if it matches.
I want it to accept the following characters: 0-9 as well as ,.()- and optionally start with a + (for international numbers). The string should not match if it has any other characters.
I tried this:
/\+?[0-9\/\.\(\)\-]/
But it matches phone numbers that have + in the middle of the number. And it matches numbers that contain alpha chars (I don't want that).
Lastly, I want to set the minimum length to 9 characters.
Any thoughts?
Thanks for any help, I'm obviously not too swift on RegEx stuff :)
Well, you're pretty close. Try this:
^\+?[0-9\/.()-]{9,}$
Without the start and end anchors you allow partial matching, so it can match +123 from the string :-)+123.
If you want a minimum of 9 digits, rather than any characters (so ---.../// isn't valid), you can use:
^\+?[\/.()-]*([0-9][\/.()-]*){9,}$
or, using a lookahead - before matching the string for [0-9/.()-]* the regex engine is looking for (\D*\d){9}, which is a of 9 digits, each digit possibly preceded by other characters (which we will validate later).
^\+?(?=(\D*\d){9})[0-9\/.()-]*$
The reason why it matches alpha character is because of the period. You have to escape it. I don't know what editor you are using for this, this is what I'll use for VIM:
^+\?[()\-\.]\?\([0-9][\.()\-]\?\)\{3,\}$
The juqeury has a plugin for US phone validation. Check this link. You can also see the regular expression in the source code.