Regex to match date with a space - regex

I currently have the following regex to match a date:
([012]?\d)[\/.-]([0123]?\d)[\/.-]([012]\d{3})\b
This will match, for example 12/24/2011. How would I make the regex so it also matches with a space between the items, 12 24 2011?

Add the space character in the character class:
[\/. -]
^

Your questions has been already answered, but I see some other issues with your regex.
Why do have the word boundary only at the end? You should also use it at the start.
Your regex would also match things like "1.3/2012". You can imporve this easily by using a backreference.
Do you need all those capturing groups?
So my suggestion would be this
\b([012]?\d)([\/. -])([0123]?\d)\2([012]\d{3})\b
^^^^^^^^^ ^^
store the first match
occurence in group 2
group 2
See it here on Regexr

Use this:
([012]?\d)[ \/.-]([0123]?\d)[ \/.-]([012]\d{3})\b
I think that you must add spaces first.

([012]?\d)([\/.-]|\s)([0123]?\d)([\/.-]|\s)([012]\d{3})\b

([012]?\d)[\/.-\s]([0123]?\d)[\/.-\s]([012]\d{3})\b

Related

Regex to find if all the characters in a word are the same specific character

I have a set of words coming in one by one like aa, ##, ???, ~~~, ?~ etc
I need a regex to find if any of these words is containing only ? or only ~.
Of the above input examples, ??? and ~~~ should match but not the others.
I tried ^[\s?]*$ and ^[\s~]*$ separately and it works, I am trying to combine them.
^[\s?||~]*$ doesn't work as it also recognizes ?~ as valid.
Any help?
You can use this regex, which looks for a string starting with a ~ or a ?, and then asserts that every other character in the string is the same as the first one using a backreference (\1):
^([~?])\1+$
Demo on regex101
You need to use backreference to achived your desired result.
If you want only ~ or ? use
^([~?])\1+$
If you want any repetitive pattern, use
^(.)\1+$
Explanation (.) or ([~?]) capturing the first charactor.
Then, \1+ checking the first charactor, one or more times (backreferencing)
You want to match lines that both start and end with any number of either a tilde or questionmark. That would be ^\(~\|?\)*$. The parentheses to make a group and the vertical bar to do the 'or' need to be backslash escaped.

RegEx: How to exclude the first two characters from selection

If you look at this text:
FIRST TEXT (IF CAPS AND IF IT ENDS WITH A PERIOD) SHOULD BE EXCLUDED. Here comes all the text we want to grab. And the ONLY problem with our current regular expression is that it also includes the period and space in front of this text. Does anyone know how to fix it so we grab from "Here comes..." and not ". Here comes..."? Thank you.
My current regular expression looks like this: (?![A-ZÆØÅ!´'/0-9\s()]+[.])[^=]*
But I simply can't figure out how to exclude the first ". " from the selection. Can anyone please help? You can try it out here:
https://regex101.com/r/UpRlOV/3
The dot and spaces are matched because your lookahead pattern does not match only up to the dot. To make sure your match does not start with . + space(s), you may consume them if they are present. An optional non-capturing group is quite handy in such situations:
(?![A-ZÆØÅ!´'\/0-9\s()]+[.])(?:\.\s*)?\K[^=]+
^^^ ^^
or, if your regex engine does not support \K match reset operator, use a capturing group:
(?![A-ZÆØÅ!´'\/0-9\s()]+[.])(?:\.\s*)?([^=]+)
^ ^
See the regex demo.
.+?\.(.+)
Is something like this what you're looking for?
this way you can just grab group 1 from the result
https://regex101.com/r/1Eo38B/1

Capture number between two whitespaces (RegEx)

I have the following data:
SOMEDATA .test 01/45/12 2.50 THIS IS DATA
and I want to extract the number 2.50 out of this. I have managed to do this with the following RegEx:
(?<=\d{2}\/\d{2}\/\d{2} )\d+.\d+
However that doesn't work for input like this:
SOMEDATA .test 01/45/12 2500 THIS IS DATA
In this case, I want to extract the number 2500.
I can't seem to figure out a regex rule for that. Is there a way to extract something between two spaces ? So extract the text/number after the date until the next whitespace ? All I know is that the date will always have the same format and there will always be a space after the text and then a space after the number I want to extract.
Can someone help me out on this ?
Capture number between two whitespaces
A whitespace is matched with \s, and non-whitespace with \S.
So, what you can use is:
\d{2}\/\d{2}\/\d{2} +(\S+)
^^^
See the regex demo
The 1+ non-whitespace symbols are captured into Group 1.
If - for some reason - you need to only get the value as a whole match, use your lookbehind approach:
(?<=\d{2}\/\d{2}\/\d{2} )\S+
Or - if you are using PCRE - you may leverage the match reset operator \K:
\d{2}\/\d{2}\/\d{2} +\K\S+
^^
See another demo
NOTE: the \K and a capture group approaches allow 1 or more spaces after the date and are thus more flexible.
I see some people helped you already, but if you would want an alternative working one for some reason, here's what works too :)
.+ \d+\/\d+\/\d+ (\d+[\.\d]*)
So the .+ matches anything plus the first space
then the \d+/\d+/\d+ is the date parsing plus a space
the capturing group is the number, as you can see I made the last part optional, so both floating point values and normal values can be matched. Hope this helped!
Proof: https://regex101.com/r/fY3nJ2/1
Just make the fractal part optional:
(?<=\d{2}\/\d{2}\/\d{2} )\d+(?:\.\d+)?
Demo: https://regex101.com/r/jH3pU7/1
Update following clarifications in comments:
To match anything (but space) surrounded by spaces and prepended by date use:
(?<=\d{2}\/\d{2}\/\d{2} )\S+
Demo: https://regex101.com/r/jH3pU7/3
Rather than capture, you can make your entire match be the target text by using a look behind:
(?<=\d\d(\/\d\d){2} )\S+
This matches the first series of non-whitespace that follows a "date like" part.
Note also the reduction in the length of the "date like" pattern. You may consider using this part of the regex in whatever solution you use.

I allow only one hyphen (-) in regex

I have a text box where i get the last name of user. How do I allow only one hyphen (-) in a regular expression?
^([a-z A-Z]*-){1}[a-z A-Z]*$
you can use negative lookahead to reject strings having more than one hyphen:
^(?![^-]+-[^-]+-)[a-zA-Z- ]+$
Matched demo on debuggex.
Another Matched demo on debuggex.
Not Matched Demo demo on debuggex.
Your regular expression allow exactly one -. but I assume that you want to mach "Smith", "Smith-Kennedy", but not "Smith-", to do this you just must move the hyphen to the second group:
^[a-z A-Z]+(-[a-z A-Z]+)?$
BTW, in almost all cases when * is used + is the better solution.
I am assuming you want up to 1 hyphen. If so, the regex you want is
^[a-z A-Z]*-?[a-z A-Z]*$
You can visualize it on www.debuggex.com.
If it matches .*-.*-, then you have more than one hyphen and such string should not be accepted
A problem with your regex is that it forces the user to put a -. You can use ? to make it optional :
^[a-z A-Z]*\-?[a-zA-Z]*$

Antimatch with Regex

I search for a regex pattern, which shouldn't match a group but everything else.
Following regex pattern works basicly:
index\.php\?page=(?:.*)&tagID=([0-9]+)$
But the .* should not match TaggedObjects.
Thanks for any advices.
(?:.*) is unnecessary - you're not grouping anything, so .* means exactly the same. But that's not the answer to your question.
To match any string that does not contain another predefined string (say TaggedObjects), use
(?:(?!TaggedObjects).)*
In your example,
index\.php\?page=(?:(?!TaggedObjects).)*&tagID=([0-9]+)$
will match
index.php?page=blahblah&tagID=1234
and will not match
index.php?page=blahTaggedObjectsblah&tagID=1234
If you do want to allow that match and only exclude the exact string TaggedObjects, then use
index\.php\?page=(?!TaggedObjects&tagID=([0-9]+)$).*&tagID=([0-9]+)$
Try this. I think you mean you want to fail the match if the string contains an occurence of 'TaggedObjects'
index\.php\?page=(?!.*TaggedObjects).*&tagID=([0-9]+)$