First occurrence of a character in string. Regular expression needed - regex

I have some CSV data like this:
1325318514,197.1,184.9,172.4,146.0,147.3,131.1,280.9,182.7,12.6,5.0,0.0,73001,65848,0
1325318536,196.2,184.2,172.1,146.3,147.1,131.1,264.9,175.6,12.6,5.0,0.0,71590,64616,0
1325318557,196.6,184.9,172.1,147.6,146.8,130.9,264.9,178.4,12.6,5.0,0.0,69607,61274,0
1325318578,196.7,184.2,172.1,148.4,146.8,130.6,255.9,174.0,12.5,5.0,0.0,74127,59221,0
....
i want to replace the first , with a space on each string but not the rest of the ,s
Any ideas on the regexp for that? Tried a few different things and just cant seem to get it to work...

your question is not very clear, I just give an example, hope it is helpful for you:
sed 's/,/ /1' <<<"1325318514,197.1,184.9,172.4,146.0,147.3,131.1,280.9,182.7,12.6,5.0,0.0,73001,65848,0
1325318536,196.2,184.2,172.1,146.3,147.1,131.1,264.9,175.6,12.6,5.0,0.0,71590,64616,0
1325318557,196.6,184.9,172.1,147.6,146.8,130.9,264.9,178.4,12.6,5.0,0.0,69607,61274,0
1325318578,196.7,184.2,172.1,148.4,146.8,130.6,255.9,174.0,12.5,5.0,0.0,74127,59221,0"
output:
1325318514 197.1,184.9,172.4,146.0,147.3,131.1,280.9,182.7,12.6,5.0,0.0,73001,65848,0
1325318536 196.2,184.2,172.1,146.3,147.1,131.1,264.9,175.6,12.6,5.0,0.0,71590,64616,0
1325318557 196.6,184.9,172.1,147.6,146.8,130.9,264.9,178.4,12.6,5.0,0.0,69607,61274,0
1325318578 196.7,184.2,172.1,148.4,146.8,130.6,255.9,174.0,12.5,5.0,0.0,74127,59221,0

In most regex implementations, use the pattern
/([^,]*),(.*)/gm
And replacement text
$1 $2
See it online at http://refiddle.com/1ot

Related

Notepad++ RegEX how do I append a character based on start of the character and before a character?

I would like to append _OLD to the end of each strings that starts with SR_ but before the symbol ' or without it
For example my text is the following:
SR_Apple
When the 'SR_APPLE' rotten, we must discard it.
I would like the find and replace to do:
SR_Apple_OLD
When the 'SR_APPLE_OLD' rotten, we must discard it.
I have tried (SR_*)+$.*(?='\s) based on what i Learned but no luck so far. Please help. Thx in Adv
For simple cases you should be able to use
Find: (\bSR_[\w]+)
Replace: $1_OLD
(\bSR_.+?)('|$) and $1_OLD$2 could also work if the text after SR_ is more complex
The lookbehind you're using is only matching the string if it ends with a ' so it won't find the text not in quotes.
regex101 is a useful tool for debugging expressions

Using regex to replace all except specific string form

I'm looking to pull data from some code, to do that I thought I could use regex.
An example of the code I have is:
If IsNumeric(varCS) And IsNumeric(varGTV) And IsNumeric(varTV) Then
logInfo("GO")
shtDst.Range("D6").Value = shtSrc.Cells(varCS, varGTV).Value
shtDst.Range("G104").Value = shtSrc.Cells(varCS, varTV).Value
I would like the result to be:
"D6"
"G104"
The regex I've tried is:
.*(?:Range\((.*)\))?.*
and replacing with:
\1
However this results in just blank lines.
I've looked at lookahead and lookbehind but those seem to require a fixed length string.
I've been using Notepad++ plus various online regex test sites to verify my results.
Have a try with (don't make Range... optional):
Ctrl+H
Find what: ^(?:.*?Range\((.+?)\).*?|.+)$
Replace with: $1
This is working with the given example.
Try replacing [\S\s]*?("[^"]*?").* with $1\r\n (Example)

Regex - Replace the first comma and last comma from the csv list but leave the middle one

I'm new to regex... but here goes.
So, Let's say I have this data.
13579,24680,13579,24680
13579,24680,13579,24680
13579,24680,13579,24680
13579,24680,13579,24680
So what I want is for it to replace the first comma, and the last comma with nothing. So I just end up with:
1357924680,1357924680
1357924680,1357924680
1357924680,1357924680
1357924680,1357924680
After it goes through.
I was thinking of a pattern somewhere along ^([^,]+),
But as you can tell, this doesn't work. Any suggestions or should I rethink what I use to do this?
Try something like this:
regex: ^([^,]+),([^,]+),([^,]+),([^,]+)$
options: multiline
replace $1$2,$3$4
This works in perl:
$match =~ s/^(.*?),(.*),(.*?)$/$1$2$3/;
Try
^(\d+),(\d+),(\d+),(\d+)
replace with
$1$2,$3$4
See it here on Regexr
Dependent on the way you access your text, you need to use the multiline modifier.
Ruby example
"13579,24680,13579,24680".gsub(/(.*?),(.*),(.*?)/,'\1\2\3')
=> "1357924680,1357924680"
and another example without using regex because it's always worth a try
"%s%s,%s%s" % "13579,24680,13579,24680".split(',')
=> "1357924680,1357924680"
kent$ echo "13579,24680,13579,24680
"|sed -r 's#(^[0-9]+),(.*?),([0-9]+)$#\1\2\3#'
1357924680,1357924680

Using regex to find any last occurrence of a word between two delimiters

Suppose I have the following test string:
Start_Get_Get_Get_Stop_Start_Get_Get_Stop_Start_Get_Stop
where _ means any characters, eg: StartaGetbbGetcccGetddddStopeeeeeStart....
What I want to extract is any last occurrence of the Get word within Start and Stop delimiters. The result here would be the three bolded Get below.
Start__Get__Get__Get__Stop__Start__Get__Get__Stop__Start__Get__Stop
I precise that I'd like to do this only using regex and as far as possible in a single pass.
Any suggestions are welcome
Thanks'
Get(?=(?:(?!Get|Start|Stop).)*Stop)
I'm assuming your Start and Stop delimiters will always be properly balanced and they can't be nested.
I would have done it with two passes. The first pass find the word "Get", and the second pass count the number of occurrences of it.
$ echo "Start_Get_Get_Get_Stop_Start_Get_Get_Stop_Start_Get__Stop" | awk -vRS="Stop" -F"_*" '{print $(NF-1)}'
Get
Get
Get
Something like this, maybe:
(?<=Start(?:.Get)*)Get(?=.Stop)
That requires variable-length lookbehind support, which not all regex engines support.
It could be made to have a max length, which a few more (but still not all) support, by changing the first * to {0,99} or similar.
Also, in the lookahead, possibly the . should be a .+ or .{1,2} depending on if the double underscore is a typo or not.
With Perl, i'd do :
my $test = "Start_Get_Get_Get_Stop_Start_Get_Get_Stop_Start_Get_Stop";
$test =~ s#(?<=Start_)((Get_)*)(Get)(?=_Stop)#$1<FOUND>$3</FOUND>#g;
print $test;
output:
Start_Get_Get_<FOUND>Get</FOUND>_Stop_Start_Get_<FOUND>Get</FOUND>_Stop_Start_<FOUND>Get</FOUND>_Stop
You should adapt to your regex flavour.

Quick Regex question

Can anybody guide me in the right direction...
I have some folder strucktures that I want to check for a trailing / slash, some of the folders don't have a trailing / and if it does not have a trailing
/ I need to add one
If I use this regex it works but it replace the last character of the folder name
Folder/name/test
folder/name/test1/
folder/name/test2/
replace(/.$/ig,"/");
This regex replaces Folder/name/tes[t]/ but will take out the t and replace it with /
Hope this makes sense...
Try something like this:
replace(/[^/]$/ig, "$0/")
replace(/(.)$/ig,"\1/");
or better
replace(/([^\\])$/ig,"\1/");
if \1 isn't a backreference in your language, then you'll have to figure that out, or tell us teh language.
The regex you made basically means this: take any character that is the last one in the string and replace it with /. What you need to do is to group the last character and then insert it again in the replacement, like this:
replace(/([^\/])$/ig,"$1/");
For more information see
http://www.regular-expressions.info/brackets.html
Without knowing the language it's difficult to post a correct answer and you can't use the code provided in a cut-and-paste fashion. Anyway I might go for this regex:
replace(/(.)\/*$/,"\1/");
This will append the trailing / only if it's not there yet.
I'm not sure which language this is for but this is how you would do it in Perl:
#! /local/bin/perl
my #data = <data>;
while (<DATA>)
{
s#[^/]\n#/\n#m;
print;
}
__DATA__
/foo/bar/
/baz/jazz
/baz/jazz
This then prints out the following:
/foo/bar/
/baz/jaz/
/baz/jazz/
The key to the regex is the "[^/]\n" This basically matches anything at the end next to to the newline. With your nomenclature, I would assume the syntax would be the following:
replace(/[^\/]\n/ig,"/");
Or if there is no newline use this:
replace(/[^\/]$/ig,"/");
I hope that helps.
I would avoid a regular expressions in this case and do something easier like:
$path = rtrim($path, '/').'/';
EDIT:
Woops, assumed it was php...