I'm very new to regex and trying to figure this out.
I'm trying to validate a string that:
starts with anyone of these abcdjklmnpqrstwz
followed by either 1 or 2 alpha
followed by 1 to 4 ints
Optional: Ends with a single alpha
here is my regex
/(^[abcdjklmnpqrstwz](:?[a-z]{1,2})) [0-9]{1,4} *([a-z]{1})/i
here is some sample of string that should be true
bab 1234 a
bab 1234
bab 123
b 123 a
click here to test
Please see the following regex:
/^[a-dj-np-twz](?:[a-z]{1,2})?\s[0-9]{1,4}(?:\s[a-z])?$/
Here is a regex demo!
You should use a ? instead of {1} (demo):
/(^[abcdjklmnpqrstwz](:?[a-z]{1,2})) [0-9]{1,4} ([a-z]?)/i
The ? specifies that the preceding block is optional.
Related
I'm a regex newbie and I've got a valid regex for SSNs:
/^(\d{3}(\s|-)?\d{2}(\s|-)?\d{4})|[\d{9}]*$/
But I now need to expand it to accept either an SSN or another alphanumeric ID of 7 characters, like this:
/^[a-zA-Z0-9]{7}$/
I thought it'd be as simple as grouping the SSN and adding an OR | but my tests are still failing. This is what I've got now:
/^((\d{3}(\s|-)?\d{2}(\s|-)?\d{4})|[\d{9}])|[a-zA-Z0-9]{7}$/
What am I doing wrong? And is there a more elegant way to say either SSN or my other ID?
Thanks for any helpful tips.
Valid SSNs:
123-45-6789
123456789
123 45 6789
Valid ID: aCe8999
I have modified your first regex also a bit, below is demo program. This is as per my understanding of the problem. Let me know if any modification is needed.
my #ids = (
'123-45-6789',
'123456789',
'123 45 6789',
'1234567893434', # invalid
'123456789wwsd', # invalid
'aCe8999',
'aCe8999asa' # invalid
);
for (#ids) {
say "match = $&" if $_ =~ /^ (?:\d{3} ([ \-])? \d{2} \1? \d{4})$ | ^[a-zA-Z0-9]{7}$/x ;
}
Output:
match = 123-45-6789
match = 123456789
match = 123 45 6789
match = aCe8999
Your first regex got some problems. The important thing about it is that it accepts {{{{}}}}} which means you have built a wrong character class. Also it matches 123-45 6789 (notice the mixture of space and dash).
To mean OR in regular expressions you need to use pipe | and remember that each symbol belongs to the side that it resides. So for example ^1|2$ checks for strings beginning with 1 or ending with 2 not only two individual input strings 1 and 2.
To apply the exact match you need to do ^1$|^2$ or ^(1|2)$.
With the second regex ^[a-zA-Z0-9]{7}$ you are not saying alphanumeric ID of 7 characters but you are saying numeric, alphabetic or alphanumeric. So it matches 1234567 too. If this is not a problem, the following regex is the solution by eliminating the said issues:
^\d{3}([ -]?)\d\d\1\d{4}$|^[a-zA-Z0-9]{7}$
I need a regular expression that would find 100 ABCDEF from input string Suite 400 - 100 ABCDEF. It should be noted that I created a regex as below but it picks the value from Suite.
[^-\s]\d.+
Just put $ at the end of your regex. $ means "end of line".
Also, replace the dot with [^-], so it will match only non-hyphens:
[^-\s]?\d[^-]+$
Fiddle: http://refiddle.com/refiddles/5b9a88ef75622d4ca9590000
Since you're trying to match a US street address, you should try matching a number followed by one or more words instead:
\d+(?:\s+[A-Za-z.]+)+
Demo: https://regex101.com/r/y6n5jD/1
I have a regular expression (built in adobe javascript) which finds string which can be of varying length.
The part I need help with is when the string is found I need to exclude the extra characters at the end, which will always end with 1 1.
This is the expression:
var re = new RegExp(/WASH\sHANDLING\sPLANT\s[-A-z0-9 ]{2,90}/);
This is the result:
WASH HANDLING PLANT SIZING STATION SERVICES SHEET 1 1 75 MOR03 MUP POS SU W ST1205 DWG 0001
I need to modify the regex to exclude the string in bold beginning with the 1 1.
Keep in mind the string searched for can be of varying length hence the {2,90}
Can anyone please advise assistance in modifying the REGEX to exclude all string from 1 1
Thank you
You may use a positive lookahead and keep the same functionality:
/WASH\sHANDLING\sPLANT\s[-A-Za-z0-9 ]{2,90}(?=\b1 1\b)/
^^^^^^^^^^^
The (?=\b1 1\b) lookahead requires 1 1 as whole "word" after your match.
See the regex demo
Also, note that [A-z] matches more than just letters.
I lost a hope to find a solution by myself..
I need a regex to find a word(w) in a text which start with plus sign (+), but ignore words which start with 2 or more plus signs,
i.e. in string
"+aaa +bbb ++ccc ddd eee+ fff++ +ggg hhh"
it should find
"aaa, bbb, ggg"
Thanks for any help
You can use following regex:
(?:\s|^)\+(\w+)
Demo : https://regex101.com/r/nU3oH3/4
This would find the three matches:
(^|[^\+])\+(\w+)
fiddle: https://regex101.com/r/vD6iQ4/2
(^|\s+)\+(\w+)
https://regex101.com/r/bR1yF7/2
MATCH 1
1. [0-0] ``
2. [1-4] `aaa`
MATCH 2
1. [4-5] ` `
2. [6-9] `bbb`
MATCH 3
1. [30-31] ` `
2. [32-35] `ggg
I have a variable like this
var = "!123abcabc123!"
i'm trying to capture all the '123' and 'abc' in this var.
this regex (abc|123) retrieve what i want but...
My question is: when i try this regex !(abc|123)*! it retrieve only the last iteration. what will i do to get this output
MATCH 1
1. [1-4] `123`
MATCH 2
1. [4-7] `abc`
MATCH 3
1. [7-10] `abc`
MATCH 4
1. [10-13] `123`
https://regex101.com/r/mD4vM8/3
Thank you!!
If your language supports \G then you may free to use this.
(?:!|\G(?!^))\K(abc|123)(?=(?:abc|123)*!)
DEMO