Split Camelcase Word into Words with PHP Preg_Match (Regular Expression)

Split camelCase word into words with php preg_match (Regular Expression)

You can also use preg_match_all as:

preg_match_all('/((?:^|[A-Z])[a-z]+)/',$str,$matches);

Explanation:

(        - Start of capturing parenthesis.
(?: - Start of non-capturing parenthesis.
^ - Start anchor.
| - Alternation.
[A-Z] - Any one capital letter.
) - End of non-capturing parenthesis.
[a-z]+ - one ore more lowercase letter.
) - End of capturing parenthesis.

Combine regular expressions for splitting camelCase string into words

These are always fun puzzles to solve; I've narrowed down the cases to just two:

  1. Detect words that start with a capital followed by lowercase letters (but not preceded by a word boundary or start of the subject) - (?<!\b)[A-Z][a-z]+

  2. Detect transitions from lowercase to uppercase - (?<=[a-z])[A-Z]

    function camelFix($str)
    {
    return preg_replace_callback('/(?<!\b)[A-Z][a-z]+|(?<=[a-z])[A-Z]/', function($match) {
    return ' '. $match[0];
    }, $str);
    }

It works for the inputs you have given; it might fail cases that I have not foreseen :)

how to convert camel case to upper english words in php

a) You can use ucwords():-

echo ucwords($string);

Output:- https://3v4l.org/sCiEJ

b) In your expected outcome spaces are there, if you want that then do:

echo ucwords(implode(' ',preg_split('/(?=[A-Z])/', 'createWebsiteManagementUsers')));

Output:- https://3v4l.org/v3KUK

RegEx to split camelCase or TitleCase (advanced)

The following regex works for all of the above examples:

public static void main(String[] args)
{
for (String w : "camelValue".split("(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])")) {
System.out.println(w);
}
}

It works by forcing the negative lookbehind to not only ignore matches at the start of the string, but to also ignore matches where a capital letter is preceded by another capital letter. This handles cases like "VALUE".

The first part of the regex on its own fails on "eclipseRCPExt" by failing to split between "RPC" and "Ext". This is the purpose of the second clause: (?<!^)(?=[A-Z][a-z]. This clause allows a split before every capital letter that is followed by a lowercase letter, except at the start of the string.

Split string at an uppercase character

<pre>
<?php
$string = "VernonClemans\nSusanPostell\nRonaldGutkowski";
foreach(explode("\n", $string) as $name) {
list($first_name, $last_name) = preg_split('/(?<=\\w)(?=[A-Z])/', $name);
print "First Name: $first_name \nLast Name: $last_name\n";
}
?>
</pre>

PHP Preg_match each word in a string to find matches with all the items in an array that contains forbidden words

The key is to use \b assertion for word-boundary:

<?php
$forbidden = ['pool', 'cat', 'rain'];

// Examples
$examples = [
// pool:
'the pool is cold', //should be TRUE - working fine
'the poolside is yellow', //should be TRUE - working fine
'the carpool lane is closed', //should be FALSE - currently failing
'the print spooler is not working', //should be FALSE - currently failing

// cat:
'the cats are wasting my time', //should be TRUE - working fine
'the cat is wasting my time', //should be TRUE - working fine
'joe is using the bobcat right now', //should be FALSE - currently failing
];

$pattern = '/\b(' . implode ('|', $forbidden) . ')/i';

foreach ($examples as $example) {
echo ((preg_match ($pattern, $example) ? 'TRUE' : 'FALSE') . ': ' . $example . "\n");
}

http://sandbox.onlinephpfunctions.com/code/f424e6c78d3b13905486f646667c8bc9d48eda3a

preg_match to find a word that ends in a certain character

This regex will do the trick:

(\d+)d (\d+)h (\d+)m (\d+)s

Each value (day, hour, minute, second) will be captured in a group.

About your regex: I don't know what do you mean by "isn't correct", but I guess it's probably failing because your regex is greedy instead of lazy (more info). Try using lazy operators, or using more specific matches (\d instead of ., for example).

EDIT:

I need them to be separate variables

After matching, they will be put in different locations in the resulting array. Just assign them to variables. Check out an example here.

If you have trouble understanding the resulting array structure, you may want to use the PREG_SET_ORDER flag when calling preg_match_all (more information here).

preg_match string from sentence if title has certain word

Try this:

$likes = preg_match("#href=\"(.*?)\".*usic#si", $data, $matches);

The first group will be your result



Related Topics



Leave a reply



Submit