i want to replace similar numbers in two strings please help me how can i do this easily
strin="1,2,3,4,5,6,7,8"
string2="1,5,8"
$arr_1 = explode(',',$strin);
$arr_2 = explode(',',$string2);
$result_array=array_unique( array_merge($arr_1, $arr_2) );
echo implode(',',$result_array);
Related
So I'm trying to split a string in several options, but those options are allowed to occur only once. I've figured out how to make it match all options, but when an option occurs twice or more it matches every single option.
Example string: --split1 testsplit 1 --split2 test split 2 --split3 t e s t split 3 --split1 split1 again
Regex: /-{1,2}(split1|split2|split3) [\w|\s]+/g
Right now it is matching all cases and I want it to match --split1, --split2 and --split3 only once (so --split1 split1 again will not be matched).
I'm probably missing something really straight forward, but anyone care to help out? :)
Edit:
Decided to handle the extra occurances showing up in a script and not through RegEx, easier error handling. Thanks for the help!
EDIT: Somehow I ended up here from the PHP section, hence the PHP code. The same principles apply to any other language, however.
I realise that OP has said they have found a solution, but I am putting this here for future visitors.
function splitter(string $str, int $splits, $split = "--split")
{
$a = array();
for ($i = $splits; $i > 0; $i--) {
if (strpos($str, "$split{$i} ") !== false) {
$a[] = substr($str, strpos($str, "$split{$i} ") + strlen("$split{$i} "));
$str = substr($str, 0, strpos($str, "$split{$i} "));
}
}
return array_reverse($a);
}
This function will take the string to be split, as well as how many segments there will be. Use it like so:
$array = splitter($str, 3);
It will successfully explode the array around the $split parameter.
The parameters are used as follows:
$str
The string that you want to split. In your instance it is: --split1 testsplit 1 --split2 test split 2 --split3 t e s t split 3 --split1 split1 again.
$splits
This is how many elements of the array you wish to create. In your instance, there are 3 distinct splits.
If a split is not found, then it will be skipped. For instance, if you were to have --split1 and --split3 but no --split2 then the array will only be split twice.
$split
This is the string that will be the delimiter of the array. Note that it must be as specified in the question. This means that if you want to split using --myNewSplit then it will append that string with a number from 1 to $splits.
All elements end with a space since the function looks for $split and you have a space before each split. If you don't want to have the trailing whitespace then you can change the code to this:
$a[] = trim(substr($str, strpos($str, "$split{$i} ") + strlen("$split{$i} ")));
Also, notice that strpos looks for a space after the delimiter. Again, if you don't want the space then remove it from the string.
The reason I have used a function is that it will make it flexible for you in the future if you decide that you want to have four splits or change the delimiter.
Obviously, if you no longer want a numerically changing delimiter then the explode function exists for this purpose.
-{1,2}((split1)|(split2)|(split3)) [\w|\s]+
Something like this? This will, in this case, create 3 arrays which all will have an array of elements of the same name in them. Hope this helps
i have some hardcoded data that looks like this:
s1 = [ ...
[-225.053,-74.050,4.801]' ...
[-82.053,68.950,4.801]' ...
[-82.053,-74.050,4.801]' ...
[-82.053,-217.050,4.801]' ...
[60.947,-74.050,4.801]' ...
];
i've got a bunch of those entries. i realized that the first 2 entries of each vector were in the incorrect order, so rather than [a,b,c] it should be [b,a,c].
i want the code to be clear, so i'd like to swap the hardcoded values.
how do i swap the values in vim using regexp?
found a solution. the following will work assuming there are no spaces:
s/\(\-\?\d\+\.\d\+\),\(\-\?\d\+\.\d\+\),\(\-\?\d\+\.\d\+\)/\2,\1,\3/gc
that's 3 times this:
\(\-\?\d\+\.\d\+\)
which matches a number of the form [optional sign]a.b
Firstly, you should know how to swap two values.
:%s/\(123\),\(456\)/\2,\1/
Secondly, you should replace to the correct regular expression.
[-225.053,-74.050,4.801]
these number split by "," so you can use this
\[\(.*\),\(.*\),\(.*\)\]
So, finally, the regular expression is
:%s/\[\(.*\),\(.*\),\(.*\)\]/[\3,\1,\2]/
Is it at all possible to dynamically generate a regular expression using values from an array in Perl?
Lets assume I have an array of keywords that I want to match on. How can I build the regex from the values in that array?
The following doesn't seem to work
### Generate regex dynamically
my #regx_array = ('apples','oranges','bananas');
my $dynanic_regx = qr/join("|",#regx_array)/;
As I'm looking for the following regex:
(?^i:apples|oranges|bananas);
But instead I end up with
(?^i:join("|",ARRAY(0x34c5924)));
Any help would be greatly appreciated.
You have a couple of things wrong. First, you're building your array incorrectly.
my #regx_array = ['apples','oranges','bananas'];
You use parentheses to create a list.
my #regx_array = ('apples','oranges','bananas');
Then do this:
my $list = join( '|', #regx_array );
my $dynamic_regx = qr/$list/i;
my #regx_array = ('apples','oranges','bananas');
my ($dynamic_regx) = map qr/$_/i, join "|", map quotemeta, #regx_array;
Even easier than the other two:
my #regx_array = qw(apples oranges bananas);
local $" = '|';
my $regex = qr/(#regx_array)/i;
$" is also known as the $LIST_SEPARATOR. And about that value:
When an array or an array slice is interpolated into a double-quoted string ... its elements are separated by this value. Default is a space.
I've got a list of words I'm using for writing a game:
words[0[0] = 'INCREDIBLE'
words[0[1] = 'SUPERB'
words[0[2] = 'SUBLIME'
words[0[3] = 'PHENOMENAL'
words[0[4] = 'BLITZKRIEG'
words[1[0] = 'EXCELLENT'
words[1[1] = 'BOFFO'
words[1[2] = 'SMASH'
words[1[3] = 'SUPREME'
words[1[4] = 'OUTSTANDING'
I want to make this into a 2d array by replacing the second '[' with ','
Obviously I can do this manually in no time at all. Nevertheless it's something I'd very much like to learn how to do with regex and notepad++. How would I identify the second '[' and then replace it without changing the adjoining numbers?
Currently I use \d+[\d+ to find it.
Just use this:
Find what: (\[\d+)\[
Replace with: $1,
If all or most [ are are in the same column, you can also use Alt to select the whole column via mouse and just enter ' to replace it in the whole marked range.
Try to replace (^words\[\d+)\[ by $1,
I used lookbehind (?<=\d)[
Lookahead and lookbehind are two very neat (and seemingly necessary) regex features.
I need to separate out a bunch of image urls from a document in which the images are associated with names like this:
bellpepper = "http://images.com/bellpepper.jpg"
cabbage = "http://images.com/cabbage.jpg"
lettuce = "http://images.com/lettuce.jpg"
pumpkin = "http://images.com/pumpkin.jpg"
I assume I can detect the start of a link with:
/http:[^ ,]+/i
But how can I get all of the links separated from the document?
EDIT: To clarify the question: I just want to strip out the URLs from the file minus the variable name, equals sign and double quotes so I have a new file that is just a list of URLs, one per line.
Try this...
(http://)([a-zA-Z0-9\/\\.])*
If the format is constant, then this should work (python):
import re
s = """bellpepper = "http://images.com/bellpepper.jpg" (...) """
re.findall("\"(http://.+?)\"", s)
Note: this is not "find an image in a file" regexp, just an answer to the question :)
do you mean to say you have that kind of format in your document and you just want to get the http part? you can just split on the "=" delimiter without regex
$f = fopen("file","r");
if ($f){
while( !feof($f) ){
$line = fgets($f,4096);
$s = explode(" = ",$line);
$s = preg_replace("/\"/","",$s);
print $s[1];
}
fclose($f);
}
on the command line :
#php5 myscript.php > newfile.ext
if you are using other languages other than PHP, there are similar string splitting method you can use. eg Python/Perl's split(). please read your doc to find out
You may try this, if your tool supports positive lookbehind:
/(?<=")[^"\n]+/