I have a string like this in matlab.
str='42 21 S'
How could I convert it into the following form?
str='42.21'
What I tried with regexprep() is the following:
regexprep(str,'S','');
regexprep(str,' ', '.')
which leaves me with this
str='42.21.'
This ought to do the trick, Matlab is not great with strings though so there's likely to be all sorts of ways to do it, not just using regexp/regexprep:
regexprep(regexp('42 21 A','\d+\s\d+','match'),'\s','.')
The regexp removes the space and the S at the end, and then the regexprep replaces the space with a period.
For simple replacements you don't have to use regexprep. You can use the much simpler strrep:
str = strrep(str, ' S', '');
str = strrep(str, ' ', '.');
If you require more general replacement rules, you should use regexprep, like David's answer for example.
Related
I would like to parse a string in MATLAB that has a prespecified format. For instance, I would like to parse the following string,
s = 'a(1,2)+b_c15_d.ext'
so that I can get the substring before '(', i.e., the substring 'a', the numbers that are between the parentheses, i.e, 1 and 2, and so on (the substring 'b' between the '+' and '_', and the substring 'd' before the extension).
I can do it using regexp and split, but is there a more convenient way of doing this?
I could do it using regexp and 'split', but I hope there is a more convenient way of doing so.
textscan offers a simple, perhaps convenient, alternative to regexp and split.
a = textscan(s,'%c(%d,%d)+%c_%c%d_%c.ext');
The results are in order as:
>> a =
'a' [1] [2] 'b' 'c' [15] 'd'
If you are going to have more than one character for your substring, %c to a modified %[] (character string) depending on your needs. For example:
a = textscan(s,'%[^(](%d,%d)+%[^_]_%[^_]_%[^.].ext')
The %[^(] above will collect characters from the string until the string ends or a '(' is reached on the string and place the result in the first cell of the output. The '(' is still on the string/stream however, so we remove it directly, by reading it, as shown.
sscanf is another option, but manipulating the output format is more involved.
Using regexp and split really isn't that inconvenient, you can do it nicely in 2 lines
r = regexp(s,'[()\+_(_c)(.ext)]', 'split');
r = r(~cellfun(#isempty, r))
>> r = {'a' '1,2' 'b' '15' 'd'}
I'm not sure if this is similar to what you'd already tried, since you didn't post any code.
Hi I have a string in oracle like this:
temp_a, temp_b, temp_c
What I want to get is :
e.temp_a, e.temp_b, e.temp_c
So I want to put an "e." before every part in this string
I searched in internet and I found examples to split the string or to replace simpler strings but nothing to guide me through my problem
select regexp_replace('temp_a, temp_b, temp_c',
'([a-zA-Z0-9_]+)(,?)',
'e.\1\2') from dual;
This should work.
I just noticed you're specifically asking for regex, but for what it's worth I would probably do it like this:
rtrim( replace( 'e.'||your_string, ', ', ', e.'), 'e.')
I have s string that looks like
txt = '"EMB","iShares J,P. Morg",110.81,N/A'
I'm using strsplit(txt,','); to break this into separate strings based on the comma delimiter. However I want to ignore the comma between the 'J' and the 'P', since it's not a delimiter; it's just part of the name.
Is there a way I can say "if a comma is between two quotes but there are other characters between the quotes, delete the comma"?
Here's an equivalent regexp one-liner:
C = regexp(txt, '("[^"]*")|([^,"]+)', 'match')
The result is a cell array with already split strings. Unfortunately, I don't have MATLAB R2013, so I cannot benchmark this versus strsplit.
A silly (but functional) answer:
inquotes=false;
keep=true(1,length(txt));
for v=1:length(txt)
if (txt(v)=='"')
inquotes=~inquotes;
elseif (txt(v)==',' && inquotes)
keep(v)=false;
end
end
txt=txt(keep);
tt=strsplit(txt,',');
This will, if you are in quotes, remove the commas so that you can use strsplit. That is what I understood you want to do, correct?
I have a link that looks like this http://site.com/numbers_and_letters/This_is_what-I-need_to-retrieve.html
I basically need to retrieve this part: This_is_what-I-need_to-retrieve
And also replace the the dashes and underscores with spaces so it would end up looking like this: This is what I need to retrieve
I'm new to regex so this is what i'm using:
(it works but has poor performance)
function clean($url)
{
$cleaned = preg_replace("/http:\/\/site.com\/.+\//", '', $url);
$cleaned = preg_replace("/[-_]/", ' ', $cleaned);
//remove the html extension
$cleaned = substr($cleaned, 0,-4);
return $cleaned;
}
What you've got isn't that bad. But maybe you can try comparing its performance to this:
preg_match('[^/]+$', $url, $match);
$cleaned = preg_replace('[-_]', ' ', $match);
EDIT:
If all you have is a hammer, everything looks like a nail.
How about avoiding regex altogether? (I presume each input is a valid URL.)
$cleaned = strtr(substr($url, strrpos($url, '/') + 1, -5), '-_', ' ');
This even removes the .html extension! (I'm making all the same assumptions you already seem to be making, i.e. that all links end in .html.) A brief explanation:
strtr translates a set of characters, e.g. -_, to respective characters in another set, e.g. spaces. (I imagine it'd be more efficient than invoking the entire regex engine.)
substr, you must know, but note that if the last argument is negative, e.g. -5, it indicates the number of characters from the end to ignore. Handy for this case, and again, probably more efficient than regex.
strrpos, of course, finds the last position of a character in a string, e.g. /.
I'm using the following regular expression to find the exact occurrences in infinitives. Flag is global.
(?!to )(?<!\w) (' + word_to_search + ') (?!\w)
To give example of what I'm trying to achieve
looking for out should not bring : to outlaw
looking for out could bring : to be out of line
looking for to should not bring : to etc. just because it matches the first to
I've already done these steps, however, to cross out/off should be in the result list too. Is there any way to create an exception without compromising what I have achieved?
Thank you.
I'm still not sure I understand the question. You want to match something that looks like an infinitive verb phrase and contains the whole word word_to_search? Try this:
"\\bto\\s(?:\\w+[\\s/])*" + word_to_search + "\\b"
Remember, when you create a regex in the form of a string literal, you have to escape the backslashes. If you tried to use "\b" to specify a word boundary, it would have been interpreted as a backspace.
I know OR operator but the question was rather how to organize the structure so it can look ahead and behind. I'm going to explain what I have done so far
var strPattern:String = '(?!to )(?<!\w) (' + word_to_search + ') (?!\w)|';
strPattern+='(?!to )(?<!\w) (' + word_to_search + '\/)|';
strPattern+='(?!to )(\/' + word_to_search + ')';
var pattern:RegExp = new RegExp(strPattern, "g");
First line is the same line in my question, it searches structures like to bail out for cases where you type out. Second line is for matching structures like to cross out/off. But we need something else to match to cross out/off if the word is off. So, the third line add that extra condition.