Supposing I have a string:
str = “ab,cd,ef”
and I want to split it into a list
lst = [“ab”,”cd”,ef”]
How can I do it best, assuming that I don’t know ahead of time how many items are in the string?
Basically I'm looking for a specman equivalent to Perl's:
$str = "ab,cd,ef";
#lst = split /,/, $str;
str_split is what you want.
From Specman 6.1 docs:
str_split(str: string, regular-exp: string): list of string
Syntax Example
var s: list of string = str_split("first-second-third", "-");
Related
I'd like to find all occurrences of strings in a long string which are placed between a set of two specific strings.
For eg.
String s = "abcdText('hello')abcd efghText('world')";
The regex pattern of strings would be Text(' and next ' and the results should be the List of strings enclosed between the pattern. Hence the expected output should be:
[hello, world]
After some searches, I found this. This explains my use case but it is in PHP and only meant to find digits.
You can try this way :
String myString = "abcdText('hello')abcd efghText('world')";
RegExp exp = RegExp(r"\('(.*?)'\)");
List<String> _list =[];
for (var m in exp.allMatches(myString)) {
_list.add(m[1].toString());
}
print(_list);
EDIT
String myString = "abcdText('hello')abcd efghText('world')";
RegExp exp = RegExp(r"Text\('(.*?)'\)");
List<String> _list =[];
for (var m in exp.allMatches(myString)) {
_list.add(m[1].toString());
}
print(_list);
I had a list with string first name and last name
val dataList = List("Narendra MODI","Amit SHA","Donald TRUMP","Ratan TATA","Abdul KALAM")
I want to print all the first from the list like Narendra,Amit,Donald,Ratan,Abdul
could you please help me on this in scala
The simplest option is to take the initial non-space characters from each string:
dataList.map(_.takeWhile(!_.isSpaceChar))
you can map over your list and use split on space and select the 1st index.
scala> val dataList = List("Narendra MODI","Amit SHA","Donald TRUMP","Ratan TATA","Abdul KALAM")
dataList: List[String] = List(Narendra MODI, Amit SHA, Donald TRUMP, Ratan TATA, Abdul KALAM)
scala> dataList.map( _.split(" ").headOption.getOrElse(None))
res2: List[java.io.Serializable] = List(Narendra, Amit, Donald, Ratan, Abdul)
I have to replace strings in a hash. I have:
hash = {"{STAY_ID}"=>"30030303", "{USER_NAME}"=>"test"}
And I have to replace it here:
str = "www.domain.com?person={STAY_ID}&user={USER_NAME}"
#=> www.domain.com?person=30030303&user=test
Also, it should work when there is a string with at least one match:
str = "www.domain.com?person={STAY_ID}"
#=> www.domain.com?person=30030303
I need some method/solution that can handle any situation like above.
Something great about the gsub method is that it can actually take a hash of mappings as the second argument, which it then uses to replace matched values. Therefore, if you regex any text between curly braces, you can do something like this.
str = "www.domain.com?person={STAY_ID}&user={USER_NAME}"
hash = {
"{STAY_ID}"=>"30030303",
"{USER_NAME}"=>"test"
}
str.gsub(/{(.*?)}/, hash) #www.domain.com?person=30030303&user=test
And then ya done!
I think, regex is not readable solution. You can use simple gsub method:
str = "www.domain.com?person={STAY_ID}&user={USER_NAME}"
hash = {"{STAY_ID}"=>"30030303", "{USER_NAME}"=>"test"}
result_str = hash.inject(str.dup) do |acc, (key, value)|
acc = acc.gsub(key, value)
end
result_str # www.domain.com?person=30030303&user=test
Need to exclude Numbers from String and returns cell arrays of strings in MATLAB
e.g str = 'abc76.5_pol0.00_Ev0.3'
output {'abc','pol','Ev'}
String is not specific to 'abc' etc, it could be an char long
Use regular expression.
str = 'abc76.5_pol0.00_Ev0.3';
C = regexp(str, '[a-zA-Z]*', 'match');
This is the solution that I found
output = regexp(str, '[^a-zA-Z]', 'split');
output(cellfun(#isempty,output)) = [];
You can also use strsplit with a RegularExpression option.
C = strsplit(str, '[^a-zA-Z]', 'DelimiterType', 'RegularExpression')
FOR EXAMPLE: Given a string... "1,2,3,4"
I need to be able to remove a given number and the comma after/before depending on if the match is at the end of the string or not.
remove(2) = "1,3,4"
remove(4) = "1,2,3"
Also, I'm using javascript.
As jtdubs shows, an easy way is is to use a split function to obtain an array of elements without the commas, remove the required element from the array, and then rebuild the string with a join function.
For javascript something like this might work:
function remove(array,to_remove)
{
var elements=array.split(",");
var remove_index=elements.indexOf(to_remove);
elements.splice(remove_index,1);
var result=elements.join(",");
return result;
}
var string="1,2,3,4,5";
var newstring = remove(string,"4"); // newstring will contain "1,2,3,5"
document.write(newstring+"<br>");
newstring = remove(string,"5");
document.write(newstring+"<br>"); // will contain "1,2,3,4"
You also need to consider the behavior you want if you have repeats, say the string is "1,2,2,4" and I say "remove(2)" should it remove both instances or just the first? this function will remove only the first instance.
Just use multiple substitutions.
s/^$removed,//;
s/,$removed$//;
s/,$removed,/,/;
This will be easier than trying to invent a single replacement that handles all those cases.
string input = "1,2,3,4";
List<string> parts = new List<string>(input.Split(new char[] { ',' }));
parts.RemoveAt(2);
string output = String.Join(",", parts);
Instead of using regex, I would do something like:
- split on comma
- delete the right element
- join with comma
Here is a perl script that does the job:
#!/usr/bin/perl
use 5.10.1;
use strict;
use warnings;
my $toremove = 5;
my $string = "1,2,3,4,5";
my #tmp = split/,/, $string;
#tmp = grep{ $_ != $toremove }#tmp;
$string =join',', #tmp;
say $string;
Output:
1,2,3,4
Javascript has improved since this question was posted.
I use the following regex to remove items from a csv string
let searchStr = "359";
let regex = new RegExp("^" + searchStr + ",?|," + searchStr);
csvStr = csvStr.replace(regex, "");
If the child_id is the start, middle or end, or only item it is replaced.
If the searchStr is at the start of the csvStr it and any trailing comma is replaced. Else if the searchStr is anywhere else in the csvStr it must be preceded with a comma so the searchStr and its preceding comma are replaced by an empty string.