Correct preg_replace format - preg-replace

What is the correct preg_replace format to replace everything (including the brackets) between (xxxxxx). Example have string (not long) link this: AAAAABBBBB (AAAAABBBBB) and all I want is AAAAABBBBB. I need to use preg_replace as the string and it's string length changes.
Thanks

Not sure if I understood your question right, but this should return the string insite the parentheses
$string = "CCCCC (AAAAABBBBB)";
return preg_replace("(.+\(|\))","",$string);
This regex will basically look for any string followed by ( and singular")" and replace them with "".

Related

Dart replace curly braces from string with relative values

I got a couple of response from a json which includes curly braces such as ${title} or {title}
How can I replace these vaules with values I preselected?
For example:
jsonString = '{title}.2019.mkv'
jsonString2 = '${title}.2019.mkv'
How can I replace the field in those Strings with values I preselected like:
var title = 'Avengers'
Maybe I should just use regex or is there a better way?
In case you need to replace part of the string you can use
'{title}.2019.mkv'.replaceFirst('{title}', 'Avengers')
Link to documentation

Make scala ignore multiple quotes in input

How do I make scala ignore the quotes inside of a String?
e.g.
val line1 = "<row Id="85" PostTypeId="1""
I want <row Id="85" PostTypeId="1" to be considered as a single string. However scala outputs error thinking that "<row Id=" is a string and everything after it is not related
Thanks in advance
val line1 = """<row Id="85" PostTypeId="1""""
Note those triple quotes ("""blahblah""") - to parse the string without escaping.

Talend String handling convert "0.12900-" string to -0.12900 in float

Need help with the below conversion in Talend:
"0.12900-" string to -0.12900 in float via Tmap expression.
I am not well versed with Java hence the difficulty.
You could try something like this :
row1.column.contains("-")?Float.parseFloat( "-"+ StringHandling.LEFT(row1.column,row1.column.length()-1)):Float.parseFloat(row1.column)
Float.parseFloat allows you to convert a string to a float type.
StringHandling.LEFT gets the first characters of a string, here the total length-1.
Ternary operator controls if your string contains "-", otherwise you just have to parse the "-" symbol

What is the most effective way in systemVerilog to know how many words a string has?

I have Strings in the following structure:
cmd, addr, data, data, data, data, ……., \n
For example:
"write,A0001000,00000000, \n"
I have to know how many words the String has.
I know that I can go over the String and search for the number of commas, but is there more effective way to do it?
UVM provides a facility to do regexp matching using the DPI, in case you're already using that. Have a look at the functions in uvm_svcmd_dpi.svh
Verilab also provides svlib, a package containing string matching functions.
A simpler option would be to change the commas(,) to a space, then you can use $sscanf (or $fscanf to skip the intermediate string and read directly from a file), assuming each command has a maximum number of words.
int code; // returns the number of words read
string str,word[5];
code = $sscanf(str,"%s %s %s %s %s", word[0],word[1],word[2],word[3],word[4]);
You can use %h if you know a word is in hex and translate it directly to a numeric value instead of a string.
The first step is to define extremely clearly what a word actually is vis. what constitutes the start of a word and what constitutes the end of the word, once you understand this, if should become obvious how to parse the string correctly.
In Java StringTokenizer is the best way to find the count of words in a string.
String sampleString= "cmd addr data data data data...."
StringTokenizer st = new Tokenizer(sampleString);
st.countTokens();
Hope this will help you :)
In java you can use following code to count words in string
public class WordCounts{
public static void main(String []args){
String text="cmd, addr, data, data, data, data";
String trimmed = text.trim();
int words = trimmed.isEmpty() ? 0 : trimmed.split("\\s+").length;
System.out.println(words);
}
}

Xstream ignore whitespace characters

I load data from XML into java classes using xstream library. The texts in several tags are very long and take more than one line. Such formatting causes that I have in Java class field text with additional characters like \n\t. Is there any way to load data from XML file without these characters?
Xml tag is declared in two lines. Opening tag is in the first line, then I have very long text, and the closing tag is declared in second line.
You can use regex or the string split method.
String string = "004-034556";
String[] parts = string.split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556
Just split your string. In your case it would be
String wantedText = parts[0];
Another solution would be to put your values into a string array, loop the array, match and remove any characters you dont want.
You can see how to match and remove Here