Regex To Break Up camelCase String PHP - php

Let say I have the following string:
getPasswordLastChangedDatetime
How would I be able to split that up by capital letters so that I would be able to get:
get
Password
Last
Changed
Datetime

If you only care about ASCII characters:
$parts = preg_split("/(?=[A-Z])/", $str);
DEMO
The (?= ..) construct is called lookahead [docs].
This works if the parts only contain a capital character at the beginning. It gets more complicated if you have things like getHTMLString. This could be matched by:
$parts = preg_split("/((?<=[a-z])(?=[A-Z])|(?=[A-Z][a-z]))/", $str);
DEMO

Asked this a little too soon, found this:
preg_replace('/(?!^)[[:upper:]]/',' \0',$test);

For instance:
(?:^|\p{Lu})\P{Lu}*

No need to over complicated solution. This does it
preg_replace('/([A-Z])/',"\n".'$1',$string);
This doens't take care of acronyms of course

Use this: [a-z]+|[A-Z][a-z]* or \p{Ll}+|\p{Lu}\p{Ll}*

preg_split("/(?<=[a-z])(?=[A-Z])/",$password));

preg_split('#(?=[A-Z])#', 'asAs')

Related

php regex start at the second occurrence of a character?

I've been fighting with this for a while, i have this string :
storage/12426--the sunflower boys--07-09-2014/Authorization letter/Authorization letter--4--09-15-2015--15-39.pdf
and I would love to only get this part 07-09-2014 which lies between the first two slashes.
So far I came up with this: --[^/]* which only gives me --the sunflower boys--07-09-2014.
How do I get what im looking for?
Use digits \d match:
/^[^/]*\/[^/]*(\d\d-\d\d-\d\d\d\d)\//
You can use a capture group within 2 negated character class :
\/[^\/]*(\d{2}-\d{2}-\d{4})[^\/]*\/
See demo https://regex101.com/r/mL3aF7/1
If the formatting is consistent, you can split on / and take the second group, then split on -- and take the third.
$date = explode('--', explode('/', $str)[1])[2];
You can use match reset \K based regex:
--[^-]*--\K[^/]*
RegEx Demo

Need some advice with regexp (php)

I have a string like this:
('Afghanistan','3',''),(' Albania','1','90 days'),('
Algeria','3',''),(' Andorra','3',''),(' Angola','3','')... etc
I need to select spaces after: ),('
Can someone help me plz?
Why regexp? Just use str_replace("),(' ","),('", $myString);
edit: ),(' was per your request. But I advise you to only look for (', because the very first entry might also contain a space, but isn't caught with the string you requested.
So use str_replace("(' ","('", $myString);
Assuming you have described your input perfectly, this should do the trick.
(?<='\),\(') *
You can use this :
$pattern = "~(?<=\Q),('\E) ~";
all between \Q and \E are seen as literals.
(?<=.....) means preceded by
Just try with following regex:
/\),\('( )/

How to use regex to append string to the matched results

I'm trying to replace 0-1. with 0-1.<br> how do i do that?
update:
Sorry for my vague question. You guys misunderstood me. '0-1.' is the pattern i want to replace, which means the pattern should be like `"/(\d)+(-)*(\d)*\./"` and the string may be '1.' '0-1.' or something that expression could represent
You can use a standard PHP function:
str_replace('0-1.', '0-1.<br>', $yourString);
How about:
preg_replace("/(\d+(?:-\d+)?\.)/", "$1<br>", $string);
You can use preg_replace like this:
preg_replace("/(0-1\.)/", "$1<br>", $string);
or, as you know the substitution already:
preg_replace("/0-1\./", "0-1.<br>", $string);

using regex and php

I have been trying to figure out how to convert string below string to multiple lines where it will add comma after two consecutive letters. Anyhelp is appreciated.
$myLine = "1234:21:3AB3459435:2343RT23432523:CD";
$myLine= preg_replace('/((?<=\[a-zA-Z]\b))/', ',', $myLine);
output would be
1234:21:3AB,
3459435:2343RT,
23432523:CD,
THanks,
jp
I like all the answers, i appreciate everyone pitching in to help and ran through all the various different ways of getting this to work. it is amazing what regexp php can do one thing so many different ways. thanks to all again!!!!
Here's something I came up with quickly.
$myLine = "1234:21:3AB3459435:2343RT23432523:CD";
$myLine= preg_replace('/([a-zA-Z]{2})/', "$1,\n", $myLine);
Outputs:
1234:21:3AB,
3459435:2343RT,
23432523:CD,
Or, if you don't want the trailing comma:
$myLine = "1234:21:3AB3459435:2343RT23432523:CD";
$myLine= preg_replace('/([a-zA-Z]{2}(?!$))/', "$1,\n", $myLine);
Outputs:
1234:21:3AB,
3459435:2343RT,
23432523:CD
$myLine = "1234:21:3AB3459435:2343RT23432523:CD";
$myLine = preg_replace('/([a-z]{2})/i', '$1,', $myLine);
I don't know where you want the new-lines, but as far as the consecutive letters go, it would be something like:
$myLine= preg_replace('/([a-zA-Z]{2})/', '$1,', $myLine);
Something like this should work for you:
preg_replace('~([a-z]{2})~i', "$1,", $myLine)
try this:
$myLine = "1234:21:3AB3459435:2343RT23432523:CD";
$myLine= preg_replace("/([a-z]{2})/i","$1,\n", $myLine);
Add {2} to make it match exactly twice.
/((?<=[a-zA-Z]{2}))/
Also, you can use \w for any word character.
/((?<=\w{2}\b))/
I'm having a bit of trouble interpreting your question. Assuming you mean you'd like your sample data of "1234:21:3AB3459435:2343RT23432523:CD" to be converted to "1234:21:3AB,3459435:2343RT,23432523:CD":
$myLine= preg_replace('/([a-zA-Z]{2})/','$1,',$myLine);
should work. The pattern matches exactly two letter characters in a row, and the parentheses around it make that match accessible as a reference in the replacement. The replacement then takes the two matched characters and just puts a comma after them. See http://us.php.net/manual/en/function.preg-replace.php for more details.
If you want the result to be multiple lines (e.g. for prettier output) just change the replacement expression to include a new line, e.g.
$myLine= preg_replace('/([a-zA-Z]{2})/','$1'.",\n",$myLine);

regular expr question

i'v got such string <>1 <>2 <>3
i want remove all '<>' and symbols after '<>' i want replace with such expression like www.test.com/1.jpg, www.test.com/2.jpg, www.test.com/3.jpg
is it possible to do with regex? i only know to find '/<>.?/'
preg_replace('/<>(\d+)/g', 'www.test.com/bla/$1.jpg', $input);
(assuming your replaced elements are just numbers. If they are more general, you'll need to replace '\d+' by something else).
str_replace('<>', 'www.test.com/', $input);
// pseudo code
pre_replace_all('~<>([0-9]+)~', 'www.test.com/$1.jpg', $input);
$string = '<>1 <>2 <>3';
$temp = explode(' ',preg_replace('/<>(\d)/','www.test.com/\1.jpg',$string));
$newString = implode(', ',$temp);
echo $newString;
Based on your example, I don’t think you need regex at all.
$str = '<>1 <>2 <>3';
print_r(str_replace('<>', 'www.test.com/', $str));
Regex's allow you to manipulate a string in any fashion you desire, to modify the string in the fashion you desire you would use the following regex:
<>(\d)
and you would use regex back referencing to keep the values you have captured in your grouping brackets, in this case a single digit. The back reference is typically signified by the $ symbol and then the number of the group you are referencing. As follows:
www.test.com/$1
this would be used in a regex replace scenario which would be implemented in different ways depending on the language you are implementing your regex replace method in.

Categories