I need to remove
[0037][user name]
combination from a sentence. In the first brackets always containing numbers
eg:
[0032]
Digit count will not exceed than 4 by any chance. In the second brackets always containing letters eg:
[first name]
anyone have an idea how to do this?
You can use preg_replace() to implement regular expression syntax and try the following expression.
$str = preg_replace('/\[\d+]\[[a-z ]+]/i', '', $str);
\[\d{1,4}\]\[[a-zA-Z ]+\]
This should do it.Replace by empty string.See demo.
http://regex101.com/r/oE6jJ1/22
$re = "/\\[\\d{1,4}\\]\\[[a-zA-Z ]+\\]/im";
$str = "asdas asdsad [1234][asd asd] asdasd";
$subst = "";
$result = preg_replace($re, $subst, $str);
Related
I have a code like : 784XX . XX could be a character or number and I need an expression to remove the last 2 characters (XX) using ( and only ) preg_replace.
How can I do that?
For example, the output of :
782A3 is 782,
0012122 is 00121,
76542A is 7654,
333333CD is 333333,
You can use substr function.
But if you will use preg_replace you can do this:
$val = preg_replace('/[\w\d]{2}$/', '', $val);
I'm pretty sure there are much easier ways to do this task, yet if we wish to use regular expressions, we would be starting with just a simple expression such as:
(.+)?(..)
if I understand the problem right, and our desired output is in this capturing group:
(.+)
Demo
$re = '/(.+)?(..)/m';
$str = '782A3
0012122
76542A
333333CD';
$subst = '$1';
$result = preg_replace($re, $subst, $str);
echo $result;
RegEx Circuit
jex.im visualizes regular expressions:
Advice
AbraCadaver's advice in the comment is much better way:
substr('784XX', 0, -2);
I am using php to scrape a webpage and get this string:
'[{endTime:"2019-06-05T17:15:00.000+10:00",startTime:"2019-06-05T17:00:00.000+10:00"}]'
which is not valid json, the key names are encapsulated ...
I use preg_replace to create valid json:
$x = '[{endTime:"2019-06-05T17:15:00.000+10:00",startTime:"2019-06-05T17:00:00.000+10:00"}]'
$j = preg_replace('/(\w+)\s{0,1}:/', '"\1":', $x);
and get this value:
'[{"endTime":"2019-06-"05T17":"15":00.000+"10":00","startTime":"2019-06-"05T17":"00":00.000+"10":00"}]'
but I want this value:
'[{"endTime":"2019-06-05T17:15:00.000+10:00","startTime":"2019-06-05T17:00:00.000+10:00"}]'
How do I solve this problem?
RegEx 1
Your original expression seems to be find, we would just slightly modify that to:
([{,])(\w+)(\s+)?:
and it might work, we are adding a left boundary:
([{,])
and a right boundary:
:
and our key attribute is in this capturing group:
(\w+)
RegEx 2
We can expand our first expression to:
([{,])(\s+)?(\w+)(\s+)?:
in case, we might be having spaces before the key attribute:
Demo
Test 1
$re = '/([{,])(\w+)(\s+)?:/m';
$x = '[{endTime:"2019-06-05T17:15:00.000+10:00",startTime:"2019-06-05T17:00:00.000+10:00"}]';
$subst = '$1"$2":';
$result = preg_replace($re, $subst, $x);
echo $result;
Test 2
$re = '/([{,])(\s+)?(\w+)(\s+)?:/m';
$x = '[{endTime:"2019-06-05T17:15:00.000+10:00",startTime:"2019-06-05T17:00:00.000+10:00"}]';
$subst = '$1"$3":';
$result = preg_replace($re, $subst, $x);
echo $result;
Output
[{"endTime":"2019-06-05T17:15:00.000+10:00","startTime":"2019-06-05T17:00:00.000+10:00"}]
Demo
RegEx Circuit
jex.im visualizes regular expressions:
use this pattern :
([{,])([^:]+):
it will find all texts which are following by { or ,
and use this for replacement:
$1"$2":
It will add a doublequote on both sides of your word.
$array[key][key]...[key]
replace to
$array['key']['key']...['key']
I managed only to add quotes to the first keyword of the array.
\$([a-zA-Z0-9]+)\[([a-zA-Z_-]+[0-9]*)\] replace to \$\1\[\'\2\3\'\]
You may use a regex that does not perform a recursive, but consecutive matching:
$re = '/(\$\w+|(?!^)\G)\[([^]]*)\]/';
$str = "\$array[key][key][key]";
$subst = "$1['$2']";
$result = preg_replace($re, $subst, $str);
echo $result;
See IDEONE demo
The regex (\$\w+|(?!^)\G)\[([^]]*)\] matches all square parenthetical substrings (capturing their contents into Group 2) (with \[([^]]*)\]) that either are right after a '$'+alphanumerics substring (due to the \$\w+ part) or that follow one another consecutively (thanks to (?!^)\G).
Shouldn't need anything fancy, just get the stuff you need then
replace in a callback.
Untested:
$new_input = preg_replace_callback('/(?i)\$[a-z]+\K(?:\[[^\[\]]*\])+/',
function( $matches ){
return preg_replace( '/(\[)|(\])/', "$1'$2", $matches[0]);
},
$input );
I want to replace all consecutive characters in each WORD if there are more than three (three being the most possible in German language, two for English so I know the output example is grammatically wrong).
Example input:
Hellooooo Louis, whaaaaaat's up pal?
Expected output:
Hellooo Louis, whaaat's up pal?
I tried to change:
preg_replace('/(\w)\1+/', '$1', $word);
to
preg_replace('/(\w)\3+/', '$1', $word);
However, it doesn't output anything.
You can use the following regex:
((\w)\2{2})\2+
See demo
Replace with $1.
IDEONE:
$re = "#((\w)\\2{2})\\2+#";
$str = "Hellooooo Louis, whaaaaaat's up pal?";
$subst = "$1";
$result = preg_replace($re, $subst, $str);
echo $result;
Output:
Hellooo Louis, whaaat's up pal?
EXPLANATION:
We capture the symbol with (\w) - it is Group 2 value. Then, we check if it is followed by the same character with \2{2} exactly 2 times, and we capture it into Group 1. Then, we match any more identical subsequent characters with the \2 backreference.
Here is a way to go:
preg_replace('/((\w)\2\2)\2+/', '$1', $word);
Also you can use \K for resetting after and replace with empty, which is a bit more efficient:
(\w)\1\1\K\1+
See regex101
I'm trying to change a bunch of decimals in a string to two decimal points. The regex seems to match it just fine. The problem is with the replace.
This is my code:
$input_lines = "-33.873293252 151.201538015999972,-33.873175 151.201689183999946";
print preg_replace("/[0-9]+(\.[0-9][0-9]?)?/", "$0 $2", $input_lines);
Which outputs decimal that I want | truncated decimals that I don't want:
-33.87 3293252 151.20 1538015999972 ,-33.87 3175 151.20 1689183999946
So I tried changing the replacement to $0. But now the replace stopped working, and is instead giving me:
-33.873293252 151.201538015999972,-33.873175 151.201689183999946
How can I rewrite my regular expression so it gives me the desired output?
Better:
preg_replace("/(?<=\.\d\d)\d+/","",$input_lines);
Replaces all trailing decimals after the first two with nothing.
([-+]?\d+(?:\.\d{2})?)(\d*)
Try this.Replace by $1.See demo.
https://regex101.com/r/vD5iH9/46
$re = "/([-+]?\\d+(?:\\.\\d{2})?)(\\d*)/m";
$str = "-33.873293252 151.201538015999972,-33.873175 151.201689183999946";
$subst = "$1";
$result = preg_replace($re, $subst, $str);