Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a collection of all uppercase address names and numbers and I want to extract just the first encountered address number for each address. The following examples show what I would like to extract from each:
80 ROSE COTTAGE -> 80
80A ROSE COTTAGE -> 80A
80 A ROSE COTTAGE -> 80 A
80ROSE COTTAGE -> 80 (accidental no-space)
[ANY OTHER TEXT] 80 ROSE COTTAGE -> 80
I have found some similar questions answered here and elsewhere on the internet, but they always deal with an address as a whole as opposed to specifically just address name and number:
Match each address from the address number to the 'street type'
regex street address match
Regular Expression: Any character that is NOT a letter or number
javascript regular expressions address number
JavaScript regex to validate an address
The last one makes reference to a lookahead, which lead me to construct a negative look ahead for any alphanumeric characters following a potential single text character(eg. 80 A) in my JavaScript regex. However without adding the alternative "digits only found" group (\d+) my fourth example above does not return just the number.
(?:\d+\s*[A-Z]?(?![A-Z0-9]))|(?:\d+))
Is there a way to combine these two groups into a single regex expression? Or is this not possible in JavaScript's regex implementation?
Any help with this would be greatly appreciared.
(\d+\s*(?:[A-Z](?![A-Z]))?)
You can try this.
See demo.
http://regex101.com/r/kM7rT8/13
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
So i want to make a regular expression to validate times between 12:00 and 22:00 but i cant get my head around making expressions and cant find any examples online that i can just swap out examples of. Can anybody help me?
You can use the following regular expression:
^((1[2-9]|2[0-1]):[0-5][0-9]|22:00)$
This is how it works:
^ and $ match start and end of string, and they are there to prevent matching also 112:009 for example (which contains 12:00).
[2-9] matches numbers between 2 and 9, and (a|b) would match either a or b.
Debuggex Demo
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Say I have a regex pattern like this:
/^\*HELLO\*/
Just looking for the string "*HELLO*". But then I completely want to change it up so I do this:
/^\*&$&^*2#H\*/
Now I'm looking for the string "*&$&^*2#H*".
How should I change my regex pattern to check for such a complex string with all those different characters?
You should escape the special characters in your pattern, wich are used as tokens by Regex, such as *,^ and $. Or you will end up with an error claiming about a wrong pattern in your regex.
This is how should be your regex: /\*&\$&\^\*2#H\*/.
Furthermore if you are searching for the string with .indexOf() or .includes() methods, you can just pass the string as it is.
str.indexOf("^\*&$&^*2#H\*");
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want a regular expression that checks if a string contains only the allowed characters. The allowed characters are alphanumeric and the special characters (),#\/\-. I used this expression, and it is working fine.
/^([A-Za-z0-9 .(),#\/\-]*)+$/
Now I don't want the string to start with space or any disallowed characters, but it can have space in the middle. Also, the string may not consist of only special characters; it should have at least one alphanumeric character.
Can someone help me understand how to adapt the regex I am using to check these additional constraints?
^(?=[a-zA-Z0-9])([A-Za-z0-9 .(),#\/-]*)+$
This should do it.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am relatively new to regex. I found this regex being used and was not able to understand how exactly it is working.
([\(]*([\w][\/]?[\w]?(%)?(,)?)*|[\/.]|[\w]([\-\*|\+|\\|\<\>|\=]([\(]*[\w][\/]?(%)?[\)]?[\)]?(,)?)+)*[\)]?)*$/
I understand most of the basic syntax but not able to understand what this means.
Any clarity on this will be appreciated! This is used in javascript so the tag.
Thanks!
The same without uneeded characters:
/(\(*(\w\/?\w?(%)?(,)?)*|[\/.]|\w([-*+\\<>=](\(*\w\/?(%)?\)?\)?(,)?)+)*\)?)*$/
This pattern can match this kind of string (or nothing):
(((a/b%,(((a/b%,///./././(((a/b%,k*((((((((P/%)),)
your regex:
([\(]*([\w][\/]?[\w]?(%)?(,)?)*|[\/.]|[\w]([\-\*|\+|\\|\<\>|\=]([\(]*[\w][\/]?(%)?[\)]?[\)]?(,)?)+)*[\)]?)*$/
can be visualized as:
Debuggex Demo
unless there is a typo in your regex, it will never match anything; the regex ends with $/ which means end of the string followed by /, unless you are matching over multiple lines. If this was homework, I would say the teacher is making a bad joke because of the $/ that doesn't usually match anything.
After some simplification you get:
(\(*(\w\/?\w?(%)?(,)?)*|[\/.]|\w([-*|+\\<>=](\(*\w\/?(%)?\)?\)?(,)?)+)*\)?)*$/
If you don't care about grouping, then this is similar:
(\(|[\/.]|(\w\/?\w?%?,?)|\w([-*|+\\<>=](\(*\w\/?%?\)?\)?,?)+)*\)?)*$/
Debuggex Demo
this takes advantage that (a*|b*)* can be simplified into (a|b)*
To learn more about regex REFER : regex101 give your code there and read explanation .
Your regex: ([\(]*([\w][\/]?[\w]?(%)?(,)?)*|[\/.]|[\w]([\-\*|\+|\\|\<\>|\=]([\(]*[\w][\/]?(%)?[\)]?[\)]?(,)?)+)*[\)]?)*$
Will match anything like
(d/f%,
something
122
But will fail to match something like
--{}
Explanation :
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can anybody suggest what would be regexp for String, which validates following conditions:
Can contain characters a-z or A-Z. At least one should be there.
Can contain (space), '(apostrophe), -(hyphen), .(dot)
Anything other than this set, such as special character or number or anything else, would be an invalid character.
Something like this should work
/^[a-zA-Z' \.\-]*[a-zA-Z]+[a-zA-Z' \.\-]*$/
Which translates to 'at least one letter surrounded by zero or more of any valid character'.
I usually don't answer gimme codez questions but hey it's Friday!
There are perhaps thousands of questions here on SO following the same pattern:
Plz help me with a regular expression for a string that
- _must_ contain at least one X
- _can_ contain Y
and the answer is usually something like
/^ Y* X [XY]* $/
or if you're fancy
/^ (?=.*X) [XY]+ $
Unfortunately, all these answers (or, rather, these questions) are wrong. The problem, as usual, is that the specs are incorrect - the asker takes some "good" examples and describes them in the question, but doesn't realize that this description also matches many "bad" cases. When taken literally, this question will be answered with an expression that only does a half of its work - yes, it does validate good cases, but it does not reject bad ones. A good expression must do both!
Example: I want to validate a telefone number, which is something like 123 or 123-456-789. So I post a question on SO:
Plz help me with a regular expression:
- must contain at least one digit
- can contain a dash
and in a few seconds I get
/^-*\d[\d-]*$/
which I test with my examples (works!) and insert in my code. On the next morning, to my deepest embarassment, someone registers on my site providing this "telefone number":
----------3-----------
The moral of the story: never validate "strings". Validate domain objects!
To answer this specific question: I can't provide you with a good regular expression until you tell me what it is for.