I want to ask about regExp, I need regExp, which work like this:
input = '569/A';
result = '569-1';
input = '569/a';
result = '569-2';
input = '569/B';
result = '569-3';
input = '569/b';
result = '569-4';
You definitely understand the pattern which I want. '/' or '\' converted in '-', Characters should be converted in numeric with proper sequence A = 1, a = 2, B=3, b=4. Uppercase on odd numeric and Lowercase on even numeric
Try to use .replace() - it will be much easier.
http://www.w3schools.com/jsref/jsref_replace.asp
.replace('/','-').replace('A','1').replace('a','2').replace('B','3').replace('b','4');
Related
I have an array a : a list of identified words to be compared and replace by empty character in an array b. newB is the result.
The value of a might vary according to an input file.
I am trying to use regexprep but it is not working well.
e.g.:
a = {'apple';'banana';'orange'}; % a might be also ‘watermelon’, ‘papaya’ etc
b = {'1 apple = 2 kiwi';'1 fig = 1 banana';'1 orange = 3 strawberry'};
newB = {' = 2 kiwi';'1 fig = ';' = 3 strawberry'};
From your example it seems like you want to remove a special word and a number, the appropriate regular expression for this is (for word = 'apple'): '\d+ apple'. Building the regular expression from all the words in a, using sprintf:
re = sprintf('\\d+ %s|',a{:}); %// adding | operator to select between expressions
re(end)=[]; %// discard the last '|'
The resulting regular expression is
re =
'\d+ apple|\d+ banana|\d+ orange'
Now the actual replacement:
newB = regexprep(b,re,'')
Resulting with
newB =
' = 2 kiwi'
'1 fig = '
' = 3 strawberry'
Imagine I have a cell array with two filenames:
filenames{1,1} = 'SMCSx0noSat48VTFeLeakTrace.txt';
filenames{2,1} = 'SMCSx0NoSat48VTrace.txt';
I want to get the filename which starts with 'SMCSx0' and contains the filterword 'NoSat48VTrace':
%// case 1
expression = 'SMCSx0';
filterword = 'NoSat48VTrace';
regs = regexp(filenames, ['^' expression '.*\' filterword '.*\.txt$'])
mask = ~cellfun(#isempty,regs);
file = filenames(mask)
it works, I get:
file =
'SMCSx0NoSat48VTrace.txt'
But for whatever reason does the change of the filterword to 'noSat48VTFeLeakTrace' doesn't get me the other file?
%// case 2
expression = 'SMCSx0';
filterword = 'noSat48VTFeLeakTrace';
regs = regexp(filenames, ['^' expression '.*\' filterword '.*\.txt$'])
mask = ~cellfun(#isempty,regs);
file = filenames(mask)
which is absolutely the same as before, but
file =
Empty cell array: 0-by-1
I'm actually use these lines in a function for months, without problems. But now I added some files to my folder which are not found, though their names are similar to before. Any hints?
It is actually supposed to work without including Trace into the filterword, which it does for the first case, that's why I put .*\ into the regex.
%// case 1
expression = 'SMCSx0';
filterword = 'NoSat48V';
... works
'^' expression '.*\'
The \ near the end makes it that \n is interpreted as a new-line character:
SMCSx0.*\noSat48VTFeLeakTrace.*\.txt$
This worked fine with the other filterword because NoSat48VTrace has an upper case N and \N is interpreted as simply N.
Get rid of the \, you don't need it.
You have an extra backslash in there:
regs = regexp(filenames, ['^' expression '.*\' filterword '.*\.txt$'])
^^^
|||
remove it and it should give the expected result.
How do you pad a number 12345-9 to display as 12345-09? I tried split and replace but they don't work on integers. If I convert it to a string, it gets rid of the numbers after the hyphen.
As Adam said, split on the hyphen, pad the number, and then rejoin.
s = "12345-9"
sp = s.split("-")
sp[1] = "%02d" % int(sp[1])
s = "-".join(sp)
print s
>>> s = '12345-9'
>>> '%s-%02i' % tuple(int(v) for v in s.split('-'))
'12345-09'
In Matlab, is it possible to create a string like:
f1-*f2-*f3-*f4-*f5-*f6
giving only as parameters:
f, 1:6 and -* ?
I tried:
for i=1:6; str = strcat(str, sprintf('f%d %s',i,'-* ')); end
but it doesn't work very well and seems ineficient for a larger number of files... Perhaps a regexp would be more suitable here?
This gives you the string with extra trailing separator:
str = sprintf('f%d-*', 1:6)
Perhaps you can just remove the last two characters from this. In general, a single sprintf for an array input is quite efficient.
strjoin for using -* as a delimiter, and strcat for combining the numbers with f:
>> strjoin(strcat('f',sprintfc('%d',1:6)),'-*')
ans =
f1-*f2-*f3-*f4-*f5-*f6
Because strcat accepts cell arrays, no loop is needed.
% //Data:
letter = 'f';
numbers = 1:6;
separator = '-*';
%// Let's go:
num = mat2cell(num2str(numbers(:)), ones(1,numel(numbers))); %// cell array
%// of strings from the numbers. Those strings may contain spaces.
%// Those will be removed later
s = strcat('f',num,'-*'); %// concatenate letter and separator to each number
s = [s{:}]; %// contatenate all
s = s(1:end-numel(separator)); %// remove last separator
s(s==' ') = []; %// remove spaces (in case of several-digit numbers)
I wanted to match the words in string with reverse order.
We wanted to put validation to prompt user, if name exists in reverse order.
For example:
If name column has the value, 'Viral,Tennis'
Now if user enters a new name with the value, 'Tennis,Viral'
Then how can we match reverse order of word using regex or some other way?
I am using C#.net for development.
You could take a look at the Regex.Split(String input, String regex) and do something like so:
String[] userEntry = Regex.Split(userString, "\\s+");
StringBuilder sb = new StringBuilder()
for (int i = userEntry.Length -1; i >= 0; i--)
{
sb.append(userEntry[i]).append(" ");
}
String result = sb.ToString();
//Do Validation
That would do the trick, however, you need to keep in mind that things will get a little bit messy if you do not want to change the order of special symbols such as the comma. You could easily remove those and do any validation without special symbols.
EDIT: It depends on what you mean by special symbols. The regex [^a-zA-z0-9]+ will match any character which is not a letter (upper or lower case) and which is also not a number. So you could easily do something like so:
string input = ...
string pattern = "[^a-zA-z0-9]+";
string replacement = "";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);
The above should yield a string which is only made from letters and digits. White spaces will also be removed.