Say I have strings: "Sports Car (45%)", or "Truck (50%)", how can I convert them to "Sports_Car" and "Truck".
I know about str_replace or whatever but how do I clip the brackets and numbers part off the end? That's the part I'm struggling with.
You can do:
$s = "Sports Car (45%)";
$s = preg_replace(array('/\([^)]*\)/','/^\s*|\s*$/','/ /'),array('','','_'),$s);
See it
There are a few options here, but I would do one of these:
// str_replace() the spaces to _, and rtrim() numbers/percents/brackets/spaces/underscores
$result = str_replace(' ','_',rtrim($str,'01234567890%() _'));
or
// Split by spaces, remove the last element and join by underscores
$split = explode(' ',$str);
array_pop($split);
$result = implode('_',$split);
or you could use one of a thousand regular expression approaches, as suggested by the other answers.
Deciding which approach to use depends on exactly how your strings are formatted, and how sure you are that the format will always remain the same. The regex approach is potentially more complicated but could afford finer-grained control in the long term.
A simple regex should be able to achieve that:
$str = preg_replace('#\([0-9]+%\)#', '', $str);
Of course, you could also choose to use strstr() to look for the (
You can do that using explode:
<?php
$string = "Sports Car (45%)";
$arr = explode(" (",$string);
$answer = $arr[0];
echo $answer;
?>
Related
I have a record separated by | symbol. I need to replace a string from one place to another in the same record:
My input looks like this:
BANG|ADAR|**285815**|MOTOR|GOOD||INDIA|2.4|SOFTWARE|285816_AKS|SAB_PART|**AKS_PN|285816**
I need to replace 285815 with the string after AKS_PN, in this case I need to replace 285815 with 285816.
With the (([^|]*\|){3})(.*) I am able to fetch 285815, need help in fetching string after AKS_PN in the same regular expression.
I am aware of how to replace 285815 with 285816. I am using PHP.
Regex solution
You need to use capturing groups. In general:
(everything_before)(interesting_part_1)(between)(interesting_part_in_the_end)
Afterwards, just put it together as you wish
(everything_before)(interesting_part_in_the_end)(between)
This leaves (interesting_part_1) out of the final string.
In your specific example this might come down to
^((?:[^|]*\|){2})([^|]*)\|(.*?AKS_PN)\|(.*)
which would need to be replaced by
$1$4|$3
See an example on regex101.com (still not sure what to do with 285815 here).
Everything in PHP:
<?php
$string = "BANG|ADAR|285815|MOTOR|GOOD||INDIA|2.4|SOFTWARE|285816_AKS|SAB_PART|AKS_PN|285816";
$regex = '~^((?:[^|]*\|){2})([^|]*)\|(.*?AKS_PN)\|(.*)~';
$string = preg_replace($regex, "$1$4|$3", $string);
echo $string;
# BANG|ADAR|285816|MOTOR|GOOD||INDIA|2.4|SOFTWARE|285816_AKS|SAB_PART|AKS_PN
?>
Non-regex solution
You don't even need a regular expression here (far too complicated), just split, switch and join afterwards:
<?php
$string = "BANG|ADAR|285815|MOTOR|GOOD||INDIA|2.4|SOFTWARE|285816_AKS|SAB_PART|AKS_PN|285816";
$parts = explode("|", $string);
$parts[2] = $parts[count($parts) - 1];
$string = implode("|", $parts);
echo $string;
?>
I want to take a string and split it (or explode it) into an array by full-stops (periods).
I used to have:
$processed_data = explode(".", $raw_data);
but this removes the full-stop.
Researching, I found preg_split, so tried:
$processed_data = preg_split('\.', $raw_data, PREG_SPLIT_DELIM_CAPTURE);
with both \. and \\.
but try as I might, I cannot find a way to properly include the full-stop.
Would anyone know the right way to do this?
The expected result is:
The string
$raw_data = 'This is my house. This is my car. This is my dog.';
Is broken into an array by full-stop, eg:
array("This is my house.", "This is my car.", "This is my dog.")
To split a string into sentences:
preg_match_all('~\s*\K[^.!?]*[.!?]+~', $raw_data, $matches);
$processed_data = $matches[0];
Note: if you want to handle edge cases like abbreviations, a simple regex doesn't suffice, you need to use nltk or any other nlp tool with a dictionary.
Can you try this.
$string = preg_replace("/\.\s?([A-Z])/", "*****$1", $raw_data);
$array = explode("*****", $string);
Lots of topics on this but i can't figure it out, looking for some tips, shouldn't be that difficult.
I have filename:
test_file_from_mpc.mp4_snapshot_13.29_[2015.05.13_21.10.11].jpg
i'm trying to use regex to replace the characters _ and then everything starting from snapshot
I got snapshot covered, but i can't seem to get how to catch all the occurances of _ to be selected
(_)(snapshot)(.*)
selects only 1 _
I read that . should select "any single character" not sure how to use this properly or if it is what i am looking for.
Any guidance would be great! (this is probably 100% a dupe but i have checked all the suggested threads without finding the solution to this seemingly easy problem!)
Can't comment yet, but for regex to match more than one occurrence, you need the g - global modifier.
/(_snapshot.*$|_|\.)/gi
https://regex101.com/r/aI7fF8/2
If you replace purely with space all matching occurences, remember to trim last space.
Here's a php sample as well
<?php
$str = "test_file_from_mpc.mp4_snapshot_13.29_[2015.05.13_21.10.11].jpg";
$res = preg_replace(array("/_snapshot.*$/", "/[_.]/"), array("", " "), $str);
print $res; // test file from mpc mp4
snapshot.*$|[_.]
You can try this.Replace by space.See demo.
https://regex101.com/r/mT0iE7/13
$re = "/snapshot.*$|[_.]/im";
$str = "test_file_from_mpc.mp4_snapshot_13.29_[2015.05.13_21.10.11].jpg";
$subst = " ";
$result = preg_replace($re, $subst, $str);
Another (potentially faster, but not prettier) way would be to use explode() & implode().
// Split string by underscores
$pieces = explode('_', $filename);
// Get the number of pieces
$n = count($pieces);
// Keep just the file extension in the last piece
$pieces[$n] = substr($pieces[$n], strpos($pieces[$n], '.'));
// Remove the other unwanted pieces
unset($pieces[$n - 1];
unset($pieces[$n - 2];
// Reassemble with spaces instead of underscores
$new_string = implode(' ', $pieces);
I've been wondering, is it possible to group every 2 words using regex? For 1 word i use this:
((?:\w'|\w|-)+)
This works great. But i need it for 2 (or even more words later on).
But if I use this one:
((?:\w'|\w|-)+) ((?:\w'|\w|-)+) it will make groups of 2 but not really how i want it. And when it encounters a special char it will start over.
Let me give you an example:
If I use it on this text: This is an . example text using & my / Regex expression
It will make groups of
This is
example text
regex expression
and i want groups like this:
This is
is an
an example
example text
text using
using my
my regex
regex expression
It is okay if it resets after a . So that it won't match hello . guys together for example.
Is this even possible to accomplish? I've just started experimenting with RegEx so i don't quite know the possibilities with this.
If this isn't possible could you point me in a direction that I should take with my problem?
Thanks in advance!
Regex is an overkill for this. Simply collect the words, then create the pairs:
$a = array('one', 'two', 'three', 'four');
$pairs = array();
$prev = null;
foreach($a as $word) {
if ($prev !== null) {
$pairs[] = "$prev $word";
}
$prev = $word;
}
Live demo: http://ideone.com/8dqAkz
try this
$samp = "This is an . example text using & my / Regex expression";
//removes anything other than alphabets
$samp = preg_replace('/[^A-Z ]/i', "", $samp);
//removes extra spaces
$samp = str_replace(" "," ",$samp);
//the following code splits the sentence into words
$jk = explode(" ",$samp);
$i = sizeof($jk);
$j = 0;
//this combines words in desired format
$array="";
for($j=0;$j<$i-1;$j++)
{
$array[] = $jk[$j]." ".$jk[$j+1];
}
print_r($array);
Demo
EDIT
for your question
I've changed the regex like this: "/[^A-Z0-9-' ]/i" so it doesn't
mess up words like 'you're' and '9-year-old' for example. But by doing
this when there is a seperate - or ' in my text, it will treat those
as a seperate words. I know why it does this but is it preventable?
change the regex like this
preg_replace('/[^A-Z0-9 ]+[^A-Z0-9\'-]/i', "", $samp)
Demo
First, strip out non-word characters (replace \W with '') Then perform your match. Many problems can be made simpler by breaking them down. Regexes are no exception.
Alternatively, strip out non-word characters, condense whitespace into single spaces, then use explode on space and array_chunk to group your words into pairs.
I have to make a regex for two choices, for exemple I have a string:
apps; chrome
I have to split the string in 2 pieces without spaces
1-> apps
2-> chrome
but the problem is that string might be "apps;chrome" (w/o space after ;)
I tried with explode
$part = explode(";", $search);
If the string is with space between characters the second piece have a space.
What I want is a regex for following cases to split them in 2 pieces
apps; chrome
apps;chrome
I hope you understand, sorry for my english :)
The trim function will help:
list($k1,$k2) = array_map("trim",explode(";",$search));
One-liner! =3
Try using trim on the various parts.
e.g.
$parts = array_map('trim', explode(';', $search));
Well, if you are sure about the separators, and you have two options, basically.
1) Using explode(';', $string) and array_map
This will explode the string and them apply trim() over the array;
$slices = explode(';', $string);
$slices_filtered = array_map("trim", $slices);
2) Using preg_split("/[,; \t\n]+/",$string);
This will split strings like "we , are; the \n champions" into {we,are,the,champions}
$slices_filtered = preg_split("/[,; \t\n]+/",$string);
** considering the 'options' won't have spaces on it; if they do, you should use some pattern like
/[,;][ ]*/
Just because you'd specified a Regular Expression ... and this should allow you to match any 2 lower-case alpha strings separated by a semi-colon, with any number (or type) of whitespace "noise".
$sFullString = "app; chrome"; //or wherever you're getting your string from
//RegExp pattern to match many strings including "app;chrome"
$sRegExp = '/^\s*([a-z]+);\s*([a-z]+)\s*$/';
//first replacement
$sAppMatch = preg_replace($sRegExp, "$1", $sFullString);
//second replacement
$sChromeMatch = preg_replace($sRegExp, "$2", $sFillString);
Just use a trim() function before treating your $parts
Why regex?
<?php
$parts = explode(";", $search);
foreach ($parts as $k => $v) {
$parts[$k]=trim($v);
}