How to replace all '\n' character after 'word' with 'comma' character - regex

Trying to replace all the \n character after the word 'key2:' pattern with comma.
Input String:
key1:value1\nkey2:value2\nvalue22\nvalue222
Expected:
key1:value1\nkey2:value2,value22,value222
Tried:
r'key2:(\n*$)' replace with ','
any suggestions on how can i replace it using regex! from https://rustexp.lpil.uk/

I don't think this can easily be done with regex, so I'd propose a simpler way:
let mut s = String::from("key1:value1\nkey2:value2\nvalue22\nvalue222");
let expected = "key1:value1\nkey2:value2,value22,value222";
let key2 = "key2";
let substr_index = s.find(key2).unwrap() + key2.len();
let commas = s[substr_index..].replace("\n", ",");
s.replace_range(substr_index.., &commas);
assert_eq!(s, expected);

Related

Matlab: using regexp to get a string that has a whitespace in between

I want to use Regex to acquire some ID's in a cellstring array, the array looks like this:
myString = '(['US04650Y1001', 'US90274P3029', 'HON WI', 'US41165F1012'])';
My pattern for regex is as follows:
pattern = '[A-Za-z0-9.^_]+';
newArr = regexp(myString, pattern,'match');
I'd like to get the ID called 'HON WI', but with my current pattern, its splitting it into two because my pattern can't deal with the whitespace properly. I would like to get the whole "HON WI", as well as my other strings, everything that's in '', these might have special characters like ^, . or _, but I don't know how to add the whitespace.
I already tried stuff like this, without success:
pattern = '[A-Za-z0-9.^_\s]+';
My new array should have, in each cell, the strings/ID's contained in myString (US04650Y1001, US90274P3029, HON WI and US41165F1012) with dimensions 1x4.
Another approach that seems to work but not entirely sure:
myString = strrep(myString,'([','');
myString = strrep(myString,'])','');
myString = regexp(myString,',','split');
myString = strrep(myString,'''','');
This seems to get me what I want, but I would like to know how can I alter the regex on my first approach.
Many thanks in advance.
You may use a mere '([^']+)' regex and use 'tokens' to get the captures:
myString = '([''US04650Y1001'', ''US90274P3029'', ''HON WI'', ''US41165F1012''])';
pattern = '''([^'']+)''';
newArr = regexp(myString, pattern,'match', 'tokens');
The newArr will look like
{
[1,1] = 'US04650Y1001'
[1,2] = 'US90274P3029'
[1,3] = 'HON WI'
[1,4] = 'US41165F1012'
}
You may option is to use lookaround assertions. The following will match any string made of alphanumeric character or underscore (\w), space (' ') or characters . or ^, that is located between quotes. This will specifically exclude the blank space next to the comma, in the separation between tokens, i.e. ', ' does not give a match.
Note that \s will match any blank space character (including tab, newline), this is why a space is preferred here:
pattern2='(?<='')[\w.^ ]+(?='')';
pattern2 =
(?<=')[\w.^ ]+(?=')
newArr = regexp(myString, pattern2,'match');
newArr'
ans =
'US04650Y1001'
'US90274P3029'
'HON WI'
'US41165F1012'

Add parameter to String#replace

I want to remove the last character from a string if it is a pipe. I have
.replace(/\|(\s+)?$/, '')
I want to add a parameter delim to replace since the last character changes. I am trying:
.replace(/\+delim +(\s+)?$/, '')
but no luck.
The code that uses this function:
rangeValues[cellRow][hn[j]] = rangeValues[cellRow][hn[j]].toString()
.split(frValues[i][0])
.join(frValues[i][1]).trim()
.replace(/\ + delim + (\s+)?$/, '');
You want to remove a last character using regex.
You want to use by changing delim in the regex.
If my understanding for your question is correct, how about using RegExp?
Modified script:
var delim = "|";
var string = "\\" + delim + "(\\s+)?$";
var regex = new RegExp(string);
rangeValues[cellRow][hn[j]] = rangeValues[cellRow][hn[j]].toString()
.split(frValues[i][0])
.join(frValues[i][1]).trim()
.replace(regex, '');
Note :
When delim is |, regex becomes /\|(\s+)?$/.
Reference:
RegExp
If I misunderstand your question, I'm sorry.

How can I remove all trailing backslashes from a string in Scala?

I want to remove all trailing backslashes ('\') from a string.
For example:
"ab" -> "ab"
"ab\\\\" -> "ab"
"\\\\ab\\" -> "\\\\ab"
"\\" -> ""
I am able to do this using below code but unable to handle the scenario where the String has only slash(es). Please let me know if this can be achieved through a different regex.
val str = """\\\\q\\"""
val regex = """^(.*[^\\])(\\+)$""".r
str match {
case regex(rest, slashes) => str.stripSuffix(slashes)
case _ => str
}
Converting my comment as an answer. This should work for removing all trailing backslashes:
str = str.replaceFirst("\\\\+$", "");
\\\\+ matches 1+ backslashes (single backslash is entered as \\\\ in Java/Scala).
While not a regex, I suggest a simpler solution : str.reverse.dropWhile(_ == '\\').reverse
Not using a regex, but you could use String.lastIndexWhere(p: (Char) ⇒ Boolean) to get the position of the last character which is not a '\' in order to substring until this character:
str.substring(0, str.lastIndexWhere(_ != '\\') + 1)
If, for some reason, you're committed to a regex solution, it can be done.
val regex = """[^\\]?(\\*)$""".r.unanchored
str match {
case regex(slashes) => str.stripSuffix(slashes)
}
You can do the same with slice function
str.slice(0,str.lastIndexWhere(_ != '\\')+1)

Regex for characters in specific location in string

Using notepad++, how can I replace the -s noted by the carats? The dashes I want to replace occurs every 7th character in the string.
11.871-2-2.737-2.00334-2
^ ^ ^
123456781234567812345678
It's pretty simple since it's only dashes:
(\S*?)-
Begin capture group.............................. (
Find any number of non-space chars... \S*
Lazily until...............................................?
End capture group...................................)
No capture find hyphen...........................-
Demo 1
var str = `11.871-2-2.737-2.00334-2`;
var sub = `$1`;
var rgx = /(\S*?)-/g;
var res = str.replace(rgx, sub);
console.log(res);
"There is a dash (right above 1) that I would like to preserve. This seems to get rid of all the dashes in the string"
The question clearly shows that there isn't a dash at the "1 position", but since there's a possibility that it's possible considering the pattern (n7). Don't have time to break it down, but I can refer you to a proper definition of the meta char \b.
Demo 2
var str = `-11.871-2-2.737-2.00334-2`;
var sub = `$1$2`;
var rgx = /\b[-]{1}(\S*?)-(\S*?)\b/g;
var res = str.replace(rgx, sub);
console.log(res);
Search for ([0-9\.-]{6,6})-
Replace with: $1MY_SEPARATOR

C# regex help, how to match this patttern

I have a string something like "[aaa][vad][adf]", i would like to use regex to capture the data in[], and chars in [] can be char and number and no length limit. I am regex noob, can anyone help me on this?
Thanks.
You can try something like this:
var data = "[asd][dfhg][asfsa243]";
var re = new Regex(#"\[([^\]]*)\]");
var matches = re.Matches(data);
for (int i = 0; i < matches.Count; i++ )
{
var m = matches[i];
Console.WriteLine(m.Groups[1]);
}
Console.ReadLine();
This outputs:
asd
dfhg
asfsa243
The regular expression \[([^\]]*)\] matches zero or more occurrences of a character that isn't the ] character and which is between a pair of square brackets ([ and ])
This regex might work for you:
\[(\w+)\]
That if you by char refers to word character
Here is my suggestion:
\[(\w+)\]
The charachter will be provided in the first group of the match, like this:
var regex = new Regex(#"\[([\w\d]*)\]");
MatchCollection matchCollection = regex.Matches(stringToTest);
foreach (Match match in matchCollection)
{
Debug.Print(match.Groups[0].Value);
}
Here are some good resources for building regex's
http://www.regexlib.com
http://regexpal.com/
Here is the regex for Alphanumeric:
^[a-zA-Z0-9]+$
Here's how to ensure there are 3:
/^([a-zA-Z0-9]){3}$/
You could try -
[^\[\]\W]+
var matches = System.Text.RegularExpressions.Regex.Matches("[aaa][vad][adf]",#"[^\[\]\W]+").Cast<Match>().Select(m => m.Value);
That should ignore any '[', ']' and non-word characters and would return 'aaa', 'vad' and 'adf' from your example string.