Regex to remove year from a string PHP - php

I have created a database of cigarette and trading cards. Each set title has a year associated with it. For example, 1943 or 2011. The year is always 4 characters long, but can be anywhere in the string.
Could someone please help me create a regex that will find the year in the string. I tried '/d{4}\b/' but it is failing.

(19|20)[0-9][0-9]
This will read in only 1900 and 2000 ranged dates.

Try this one :
/\b\d{4}\b/
it will match 4 digits embeded with non-words

d{4}\b will match four d's at a word boundary. You forgot the backslash in the character class: should be \d{4}\b. Depending on the input data you may also want to consider adding another word boundary (\b) at the beginning.

Here is a full solution:
$stringWithYear = '1990 New York Marathon';
$stringNoYear = preg_replace('/(19|20)[0-9][0-9]/', '', $stringWithYear);
echo trim($stringNoYear); // outputs 'New York Marathon'

This too works.. preg_match("/^1[0-9]{3}$/",$value))
Checks year of only starting with 1. You could change according to your requirement..

Related

Stripping down Phonenumber (mobile)

Is there a function or a easy way to strip down phone numbers to a specific format?
Input can be a number (mobile, different country codes)
maybe
+4917112345678
+49171/12345678
0049171 12345678
or maybe from another country
004312345678
+44...
Im doing a
$mobile_new = preg_replace("/[^0-9]/","",$mobile);
to kill everything else than a number, because i need it in the format 49171 (without + or 00 at the beginning), but i need to handle if a 00 is inserted first or maybe someone uses +49(0)171 or or inputs a 0171 (needs to be 49171.
so the first numbers ALWAYS need to be countryside without +/00 and without any (0) between.
can someone give me an advice on how to solve this?
You can use
(?:^(?:00|\+|\+\d{2}))|\/|\s|\(\d\)
to match most of your cases and simply replace them with nothing. For example:
$mobile = "+4917112345678";
$mobile_new = preg_replace("/(?:^(?:00|\+|\+\d{2}))|\/|\s|\(\d\)/","",$mobile);
echo $mobile_new;
//output: 4917112345678
regex101 Demo
Explanation:
I'm making use of OR here, matching each of your cases one by one:
(?:^(?:00|\+|\+\d{2})) matches 00, + or + followed by two numbers at the beginning of your string
\/ matches a / anywhere in the string
\s matches a whitspace anywhere in the string (it matches the newline in the regex101 demo, but I suppose you match each number on its own)
\(\d\) matches a number enclosed in brackets anywhere in the string
The only case not covered by this regex is the input format 01712345678, as you can only take a guess what the country specific prefix can be. If you want it to be 49 by default, then simply replace each input starting with a single 0 with the 49:
$mobile = "01712345678";
$mobile_new = preg_replace("/^0/","49",$mobile);
echo $mobile_new;
//output: 491712345678
This pattern (49)\(?([0-9]{3})[\)\s\/]?([0-9]{8}) will split number in three groups:
49 - country code
3 digits - area code
8 digits - number
After match you can construct clean number just concatnating them by \1\2\3.
Demo: https://regex101.com/r/tE5iY3/1
If this not suits you then please explain more precisely what you want with test input and expected output.
I recommend taking a look at LibPhoneNumber by Google and its port for PHP.
It has support for many formats and countries and is well-maintained. Better not to figure this out yourself.
https://github.com/giggsey/libphonenumber-for-php
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$usNumberProto = $phoneUtil->parse("+1 650 253 0000", "US");

Having a little trouble with my RegEx today

I am having a bit of a time with my RegEx today
\('[\d',]+
In the string:
INSERT INTO `order_status_histories` VALUES ('3602','52efabe9-5f8c-4512-a994-3227c63dd20e','1','','Order recieved','2014-02-03 16:47:05','2014-02-03 16:47:05'),('3603','52eff713-54fc-4be0-9389-68d5c63dd20e','1','','Order recieved','2014-02-03 22:07:47','2014-02-03 22:07:47'),('3604','52effd1a-bc14-4095-97fd-6d46c63dd20e','1','','Order recieved','2014-02-03 22:33:30','2014-02-03 22:33:30')
As you can see this is an insert statement, however, that 1st value is the ID of the record, which I do not need inserted, so I am attempting to find all of them, and simply blank them out... but I need to #1 get that number, the 2 ' characters, and the , after it in order to do so... so I though that I would start with the opening (.
The regex I posted in here is grabbing what I need, but a bit extra... it seems to be grabbing this ('3670','5304 (for instance in that first insertable record)
How can I do what I need here?
What about \('\d+', - so explicitly looking for digits, then ' then ,
the character class [\d',] isn't doing what you want - its matching both the opening quote of the second field and the decimal digits after it until you get to a letter

regular expressions multiple statements

I have half my problem working. The problem is: I need to match words that are either 7 letters long and starting with st OR 9 letters long ending with tion. I have code that works for the first half of the question: st\w{5}\s. This will match a 7 letter word such as 'startin' in the example: start startin starting.
However I cant seem to add the second half. (st\w{5}\s)|(tion\w{5}) Does not work in trying to find 'startin' and 'attention' out of: start startin starting attention.
Thanks.
You'll want to look for the word boundaries \b(?:(st\w{5})|(\w{5}tion))\b
use word boundaries, for example:
\b(st[a-z]{5}|[a-z]{5}tion)\b

Regex add space after colon

I'm trying to add a space between the colon and the character or number if there is no space already:
this is my sentence:hi it's midnight
this is my sentence:2012 was a good year.
this sentence should be ignored: ignore me
And I want it to read:
this is my sentence: hi it's midnight
this is my sentence: 2012 was a good year.
this sentence should be ignored: ignore me
I've tried this with no success.
Regex to insert space in vim
I can get the ":X" or ":0" using this (it doesn't match the space after the colon, which is good):
:[^\s]
But if I replace with:
: $0
It ends up like:
this is my sentence: :hi it's midnight
this is my sentence: :2012 was a good year.
I'm using http://www.gskinner.com/RegExr/ to test the pattern.
I'll be using PHP preg_replace function
Any help is much appreciated.
Thanks in advance.
You can just replace :\s* with :<pretend this is a space>:
preg_replace('/:\s*/', ': ', $text)
:\s* matches : followed by any number of spaces (or no spaces).
Here are two substitution commands to be issued in VIM:
%s/:/:<space>/g
%s/:<space><space>/:<space>/g
Of course, it is an overkill, that is, the number of commands to be issued is two, not one.
Here is two-in-one command:
:%s/:<space>*/:<space>/g
<space> is used to make it more clear as the whitespace inserted by pressing the spacebar is not printable.
Another approach to work around non-printability of space for searching:
:%s/:\s*/:<space>/g

Replace Date format in a string provided using regular expression

I am trying to change a string which may have a date inside e.g.
"This is the test string with 22/12/2012. 23/12/12 could anywhere in the string"
I need to change above string so that date are in the format d-m-y i.e.
"This is the test string with 22-12-2012. 23-12-12 could appear anywhere in the string"
EDIT:
Please note that the date will could changed in terms of years i.e. 2012 or 12 could be used at time i.e 20/06/2012, 20/06/12. Only year could be 2 or 4 digits, rest will be same.
Any help will be highly appreciated.
Cheers,
Use preg_replace like this:
$repl = preg_replace('~(\d{2})/(\d{2})/(\d{2,4})~', '$1-$2-$3', $str);
Live Demo: http://ideone.com/7HDNZa
$string = preg_replace("/([0-9]{2})\/([0-9]{2})\/([0-9]{2,4})/", "$1-$2-$3", $string);
The regex will find 3 lots of 2 numbers (or 2x2 + 1x4) separated by /'s and replace them with the same numbers separated by -'s.
You could try something like this:
preg_replace('~\b([0-2]?[1-9]|3[01])/(0?[1-9]|1[0-2])/(?=(?:\d\d|\d{4})\b)~', '$1-$2-', $str);
Should match valid dates only. Does match dates where the prefix 0 is not present, e.g. 4/16/13 if this is not desierable, remove the two first question marks (in [0-2]? and 0?)

Categories