regex for comma separated dates in php - regex

I have a problem to set regular expression for multiple dates with comma separator.
I have dates like as :
2017-03-25, 2017-03-27, 2017-03-28
please help me guys.....
i am trying to set php validation for selecting multi dates (jquery calender).
my regex is :
$value = "2017-03-25, 2017-03-27, 2017-03-28";
preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])([0-9])*$/",$value)

You are matching only a single date with 0+ digits after it with your regex.
You may use the following fix:
^([0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01]))(?:,\s*(?1))*$
See the regex demo
Details:
^ - start of string
([0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])) - Group 1 matching and capturing a date-like substring
(?:,\s*(?1))* - zero or more sequences of:
, - comma
\s* - 0+ whitespaces (remove * to only match one, or use ? to match 1 or 0 whitespaces)
(?1) - recurse Group 1 subpattern
$ - end of string.

Related

Pattern matching with regex

I am trying to make a regex pattern which matches a word/character a space and then comma seperated values with or without spaces before and after. I have found difficulty making a pattern which did this and was wondering if someone could help me.
Example of what it should match:
ages 19, 43,91
I was trying something like this, "(^[^\s])([^,]+)+", but it only matched the first one.
You can try pattern:
\S+(?:\s*\d+\s*,?)+
Regex demo.
\S+ - this will match one or more non-whitespace characters
(?:\s*\d+\s*,?)+ - non-capturing group.
\s* - match 0 or more whitespace characters
\d+ - match 1 or more digits
\s* - match 0 or more whitespace characters
,? - optionally match ,
+ - You may repeat this non-capturing group 1 or more times

SAS: Remove duplicated expressions from given list using REGEX

I would like to remove duplicated expressions from a given string using SAS code. Each expression is delimited by a space and respects the following REGEX /[A-Z]_\d{2}.\d{2}(.[a-z])?/.
Here is the code:
data want;
text = "X_99.99.a X_99.99.a A_12.00 A_12.00 A_13.00 A_12.00 X_99.99.a";
do i=1 to countw(text);
Nondups=prxchange('s/\b(\w+)\s\1/$1/',-1,compbl(text));
end;
run;
The desired result should be:
Nondups ="X_99.99.a A_12.00 A_13.00"
What should be the regular expression to be used inside the function prxchange?
Any help appreciated.
You may use
Nondups=trim(prxchange('s/\s*([A-Z]_\d{2}\.\d{2}(?:\.[a-z])?)(?=.*\1)//',-1, text));
See the regex demo
The pattern matches:
\s* - 0+ whitespaces
([A-Z]_\d{2}\.\d{2}(?:\.[a-z])?) - Group 1:
[A-Z] - an uppercase ASCII letter
_ - an underscore
\d{2} - two digits
\. - a dot (must be escaped)
\d{2} - two digits
(?:\.[a-z])? - an optional group matching 1 or 0 sequences of a . and a lowercase ASCII letter
(?=.*\1) - a positive lookahead that requires any 0+ chars other than line break chars, as many as possible, up to the value stored in Group 1 immediately to the right of the current location.

Regex select 3 group

How can I select the third group of numbers using Regex.
With the following string.
21|2|964|Texto 02
I want to select only 964.
I only managed to extract all the digit chunks with \d+ regex.
Thanks.
If you cannot split with | and get Item 3 from the resulting array, you may use
^(?:[^|]*\|){2}\K\d+
See the regex demo.
Alternatively, use
^(?:[^|]*\|){2}(\d+)
and grab Group 1 value. See another regex demo.
Details
^ - start of string
(?:[^|]*\|){2} - 2 sequences of:
[^|]* - any 0+ chars other than |
\| - a literal | symbol
\K - a match reset operator discarding the text matched so far
\d+ - 1 or more digits.

Matching Word Regex

Hello i want to match with regex this word
(Parc Installé)
from this text:
31/1/2017 17:19:23,4245986,ct0001#Intotel.int,Parc Installé,100.100.30.100
I did this regex ',[A-Za-zA-zÀ-ú+ \/\w+0-9._%+-]+,'
But the result is : 4245986 ans Parc Installé.
How can i match only Parc Installé
You may try a regex based on a lookahead that will require a comma and digits/commas after it up to the end of string:
[^,]+(?=\s*,[\d.]+$)
See this regex demo
Details:
[^,]+ - 1 or more chars other than ,
(?=\s*,[\d.]+$) - a lookahead requiring
\s* - zero or more whitespaces
, - a comma
[\d.]+ - 1+ digits or dots up to...
$ - ... the end of string
To make it a bit more restrictive, you may replace the lookahead with (?=\s*,\d+(?:\.\d+){3}$) to require 4 sequences of dot-separated 1+ digits. See this regex demo.
If a lookahead is not supported (case with a RE2 engine), you might want to use a capturing group based solution:
([^,]+)\s*,[\d.]+$
Here, the part within (...) will be captured into Group 1 and will be accessible via a backreference or a function like =REGEXEXTRACT in Google Spreasheets that only retrieves the contents of a capturing group if the latter is present in the pattern.

Nested regex replacement

I need to create the laravel migrations, so I have converted my SQL script to a laravel migration format using "replacement in files" with regular expressions from Sublime Text.
My problem is that i have to replace in the following string the '#' character by the 'tablename' in about 70 tables:
Schema::table('tablename', function($table) {
$table->dropForeign('#_columnname_foreign');
});
Actually I can do this using the following expression:
(Schema::table\('([a-z]+)',[\s]*function\(\$table\)[\s]*{[\s]*\$table->dropForeign\(')#(_[a-z_]+'\);)
And in the replace field:
$1$2$3
but I don't know how to do when the table has more than one fk:
Schema::table('tablename1', function($table) {
$table->dropForeign('#_field1_foreign');
$table->dropForeign('#_field2_foreign');
$table->dropForeign('#_field3_foreign');
$table->dropForeign('#_field4_foreign');
$table->dropForeign('#_field5_foreign');
$table->dropForeign('#_field6_foreign');
});
I have been using this site to validate my regular expressions RegExr
It is not an easy task for a regex in Sublime Text. The only way to do it with a regex is to make sure you capture the function singature with the optional number of table-dropForeign lines (matched lazily), and replace #s on the next line.
The regex below requires clicking Replace All multiple times until all matches are found.
(Schema::table\('([a-z0-9]+)',\s*function\(\$table\)\s*{(?:\s*\$table->dropForeign\('[a-z0-9]+_\w+'\);)*?\s*\$table->dropForeign\(')#(_\w+'\);)
Replacement is $1$2$3. See this regex demo, where you may replace the # in the second block manually with the table name and see how the match goes further.
Details:
(Schema::table\('([a-z0-9]+)',\s*function\(\$table\)\s*{(?:\s*\$table->dropForeign\('[a-z0-9]+_\w+'\);)*?\s*\$table->dropForeign\(') - Group 1 capturing:
Schema::table\(' - literal Schema::table(' substring
([a-z0-9]+) - Group 2 capturing 1+ alphanumerics (do not check Match Case option to also match uppercase ASCII letters)
',\s* - a comma and 0+ whitespaces
function\(\$table\) - a literal text function($table)
\s* - 0+ whitespaces
{ - a literal { (in SublimeText 2, it requires escaping)
(?:\s*\$table->dropForeign\('[a-z0-9]+_\w+'\);)*? - 0+ sequences, but as few as possible, matching:
\s*\$table->dropForeign\(' - 0+ whitespaces and then a literal text `$table->dropForeign('
[a-z0-9]+_\w+ - 1+ alphanumerics, _ and 1+ digits, letters or underscores (\w+)
'\); - a literal substring ');
\s* - 0+ whitespaces
\$table->dropForeign\(' - a literal text $table->dropForeign('
# - a matched # symbol to be replaced
(_\w+'\);) - Group 2 capturing:
_ - an underscore
\w+ - 1 or more letters, digits or underscores
'\); - a literal substring ');
NOTE: The issue I thought I found was related to an unescaped { that causes a regex failure in Sublime Text 2. In Sublime Text 3, the { in the regex does not have to be escaped.