Regex To Break String Apart By Capitalzation - php

So I have a regex that breaks a string apart that assumes camelCase or PascalCase and converts it into lowercase_with_underscores. That regex looks like this (php):
strtolower(preg_replace('/(?!^)[[:upper:]]/','_\0', $string));
I want to modify this so that it will be able to also break up the string where it assume a string of capitalizes in a row as one unit. For example, I would to be able to break up the following strings:
'GUID' => 'guid'
'SOME_VALUES' => 'some_value'
'someThingELSE' => 'some_thing_else'
Any suggestions on how to modify the regex to do this?

How about:
$result = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $string));

Related

Make a negative lookahead case-insensitive

I have the following expression:
$exp = "/^(?!.*?that).*$/";
which is meant to match any line that does not contain "that".
I have the following three sentences:
$str = array(
"I like this sentence.", #line1
"I like that sentence.", #line2
"I link THAT sentence." #line3
);
The match is case-sensitive and therefore only lines 1 and 3 are matched. So far so good.
However, I would like to make it case-insensitive, so that it only matches line 1. I have tried with an inline modifier, i.e. "(?-i ... )":
$exp = "/^(?!.*?(?i:that)).*$/";
and as a flag, i.e. "/ ... /i":
$exp = "/^(?!.*?that).*$/i";
but to no avail.
I run the search with the following loop:
foreach($str as $s) {
preg_match_all($exp, $s, $matches);
var_dump($matches);
}
with output:
array (size=1)
0 =>
array (size=1)
0 => string 'I like this sentence.' (length=21)
array (size=1)
0 =>
array (size=0)
empty
array (size=1)
0 =>
array (size=1)
0 => string 'I link THAT sentence.' (length=21)
and an online demo is available here: https://regex101.com/r/bs9rzF/1
I would grateful for any tips about how I can make my regular expression case-insensitive.
EDIT: I was incorrectly using "?-i" instead of "?-i", as some contributors correctly point out. Fixed now.
Your first regex ^(?!.*?that).*$ has nothing to do with case sensitivity as you are not using any modifier for case insensitivity.
The regex matches first and third sentence because your regex is saying that there shouldn't be a word that (case sensitive here) in the sentence, which is true for first and third sentence (In third sentence you have THAT which is not same as that)
To match only the first sentence, you can use the inline modifier (?i) like
(?i)^(?!.*?that).*$
See here
BTW, your /^(?!.*?that).*$/i regex is also correct.
You were close:
^(?!.*?(?i)that).*$
See a demo on regex101.com. In your expression ((?-i)) you were turning the modifier off.

Regular Expression on PHP

I have a problem in splitting a string using regex.
I have searched about regex to split string on uppercase word, but what I need is to split string like in the following example.
Having this example data:
This is First SentenceThis is Second Sentence
... the string should be split like this:
This is First Sentence
This is Second Sentence
Anyone know the solution for this?
You can use the \K token combined with a lookahead assertion.
$str = 'This is First SentenceThis is Second Sentence';
$results = preg_split('~[a-z]\K(?=[A-Z])~', $str);
print_r($results);
Or utilize both look-behind and lookahead assertions:
$results = preg_split('~(?<=[a-z])(?=[A-Z])~', $str);
Output
Array
(
[0] => This is First Sentence
[1] => This is Second Sentence
)

How to remove unneeded parenthesis in a string?

I have a string containing a mathematical expression, like (21)*(4+2). For the purposes of computing, I need to "simplify" it so that it doesn't contain any number between expression (i.e. (21)*(4+2) => 21*(4+2)). I have no idea of how to do it (I thought of something with regex replaces, but I'm not very good at handling it).
you can do an algorithm something like this:
$str = "(21)*(4+2)";
//split above to array of characters
$arr = str_split($str);
foreach($arr as $i => $char) {
if character is opening parenthesis {
get all characters in a string until closing parnethesis is found
endif }
if the string you received from above contains only digits
(means it has no expression i.e. +,-,/,%,*) then remove the first and last
characters of the above string which are the parenthesis and append the
string to the final string.
}
Okay, it seems to me that I accidently solved the problem(so far, the preg_replace works for me):
echo preg_replace( "/\((\d+)\)/", "$1", $eq );
It doesn't take into account the decimals, I think. The sample equation and output it generates is here on codepad.
For decimals, I used a [\d\.]+ in the regex. It seems to be working.
echo preg_replace( "/\(([\d\.]+)\)/", "$1", $eq );
Another link.

Regex escape specific characters

I'm using preg_split to make array with some values.
If I have value such as 'This*Value', preg_split will split the value to array('This', 'Value') because of the * in the value, but I want to split it to where I specified, not to the * from the value.How can escape the value, so symbols of the string not to take effect on the expression ?
Example:
// Cut into {$1:$2}
$str = "{Some:Value*Here}";
$result = preg_split("/[\{(.*)\:(.*)\}]+/", $str, -1, PREG_SPLIT_NO_EMPTY);
// Result:
Array(
'Some',
'Value',
'Here'
);
// Results wanted:
Array(
'Some',
'Value*Here'
);
The [ and ] are interpreted as character classes, so any character inside them matches. Try this one, but don't split on it, use preg_match and look in the match's captured groups.
"/(\{([^:]*)\:([^:]*)\})+/"
Original answer (which does not apply to the OP's problem):
If you want to escape * in your values with \ like this\*value, you can split on this regex:
(?<!\\)\*
Your current regular expression is a little... wild. Most special characters inside a character class are treated literally, so it can be greatly simplified:
$str = "{Some:Value*Here}";
$result = preg_split("/[{}:]+/", $str, -1, PREG_SPLIT_NO_EMPTY);
And now $result looks like this:
array(2) {
[0] => string(4) "Some"
[1] => string(10) "Value*Here"
}
The correct and safest solution to your problem is to use preg_quote. If the string contains chars that shall not be quoted, you need to str_replace them back after quoting.

Split text using multiple delimiters into an array of trimmed values

I've got a group of strings which I need to chunk into an array.
The string needs to be split on either /, ,, with, or &.
Unfortunately it is possible for a string to contain two of the strings which needs to be split on, so I can't use split() or explode().
For example, a string could say first past/ going beyond & then turn, so I am trying to get an array that would return:
array('first past', 'going beyond', 'then turn')
The code I am currently using is
$splittersArray=array('/', ',', ' with ','&');
foreach($splittersArray as $splitter){
if(strpos($string, $splitter)){
$splitString = split($splitter, $string);
foreach($splitString as $split){
I can't seem to find a function in PHP that allows me to do this.
Do I need to be passing the string back into the top of the funnel, and continue to go through the foreach() after the string has been split again and again?
This doesn't seem very efficient.
Use a regular expression and preg_split.
In the case you mention, you would get the split array with:
$splitString = preg_split('/(\/|\,| with |\&/)/', $string);
To concisely write the pattern use a character class for the single-character delimiters and add the with delimiter as a value after the pipe (the "or" character in regex). Allow zero or more spaces on either side of the group of delimiters so that the values in the output don't need to be trimmed.
I am using the PREG_SPLIT_NO_EMPTY function flag in case a delimiter occurs at the start or end of the string and you don't want to have any empty elements generated.
Code: (Demo)
$string = 'first past/ going beyond & then turn with everyone';
var_export(
preg_split('~ ?([/,&]|with) ?~', $string, 0, PREG_SPLIT_NO_EMPTY)
);
Output:
array (
0 => 'first past',
1 => 'going beyond',
2 => 'then turn',
3 => 'everyone',
)

Categories