Insert a quote in a string [closed] - regex

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 months ago.
Improve this question
I'm looking to insert a quote in a string, but keep everything else. So, an example string:
' "2020-10-10",8000,"Hello" '
I want to put quotes around 8000 (or whatever number is there). so:
' "2020-10-10","8000","Hello" '
How would I do that in regex?

I'm not an expert on regex but you can do it you just have to do it twice. Because I couldn't figure out a way to look for ",char or char,".
function test() {
try {
let a = ' "2020-10-10",8000,"Hello" ';
a = a.replace(/,/g,'","');
a = a.replace(/""/g,'"');
console.log(a);
}
catch(err) {
console.log(err);
}
}
7:26:23 AM Notice Execution started
7:26:23 AM Info "2020-10-10","8000","Hello"
7:26:23 AM Notice Execution completed

Related

Linux Replace a particular string with consecutive numbers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
CONFIG.CONFIG
create state file${number_of_edges} {
{interval=60
idle_interval_notification="DISABLED"}
}
create state file${number_of_edges} {
{interval=60
idle_interval_notification="DISABLED"}
}
I have file which has content like above. I want to replace ${number_of_edges} string with consecutive numbers like 0,1,2 and so on
EXPECTED OUTPUT
create state file0 {
{interval=60
idle_interval_notification="DISABLED"}
}
create state file1{
{interval=60
idle_interval_notification="DISABLED"}
}
create state file2 {
{interval=60
idle_interval_notification="DISABLED"}
}
I got the solution.
Command -
awk -vRS=edge '{$0=n$0;ORS=RT}++n' FILE
A simple awk script:
awk 'sub("\\${number_of_edges}",cnt+1, $0){cnt++}1' input.txt
Explanation:
sub("\\${number_of_edges}",cnt+1, $0) For each input line, substitute, ${number_of_edges} with variable (cnt + 1)
{cnt++} If substituted more than 0, increment variable cnt
1 output each input line

How to use regex for a persons name? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Basically here is my code and I cannot find the problem with this, So I'm looking for advice.
name = raw_input('What\'s your name? ')
if not re.match(r'[A-Za-z- ]+', name):
print 'Invalid name.\n' #error message
You must need to add end of the line anchor. Without the anchor foo? would be considered as a valid one. That is, it won't print the message Invalid name for this name.
if not re.match(r'[A-Za-z- ]+$', name):
print 'Invalid name.\n'
Example:
>>> s = 'foo?'
>>> if not re.match(r'[A-Za-z- ]+', s):
print('Invalid name.\n')
>>> if not re.match(r'[A-Za-z- ]+$', s):
print('Invalid name.\n')
Invalid name.

Outputting a string result from a list [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a list of items of a custom data type Film = String String Int, where the strings are the name and director and Int is the year of release.
What's the best way I would go about making a function that outputs a String or set of strings (doesn't matter how long) which show the information like:
Title: (film title) Director: (director) Released: (released)
You need to define a function that creates a String if given a Film as
an input, e.g.:
data Film = Film String String Int
instance Show Film where
show (Film t d r) =
"Title: (" ++ t ++ ") Director: (" ++ d ++ ") Released: (" ++ show(r) ++ ")"
You can read up on type classes and user-defined show here and here.

compare 2 arrays and get the values which are not matched :using perl map command or loops simply [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
arr1 = 1,2,3,4,5;
arr2 = 1,2,3;
want to compare and output as arr3=4,5;
Please help
thanks in advance
arry::utils error out, looks like some problem with the package, so that option is ruled out.
sub diff_array {
my ($a1, $a2) = #_;
my %h;
#h{#$a2} = ();
return grep !exists $h{$_}, #$a1;
}
my #arr1 = (1,2,3,4,5);
my #arr2 = (1,2,3);
my #arr3 = diff_array(\#arr1, \#arr2);

regex remove seconds and milliseconds [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is linked the my previous question, regex to add hypen in dates.
I would now like to be able to remove the seconds and milliseconds/change it to zero using gsub again as well
i.e. something like:
x <- c("20130603 00:00:03.102","20130703 00:01:03.103","20130804 00:03:03.104")
y <- gsub([REGEX PATTERN TO MATCH],[REPLACEMENT PATTERN TO INSERT HYPHEN and REMOVE SECONDS] ,x)
> y
[1] "2013-06-03 00:00:00" "2013-07-03 00:01:00" "2013-08-04 00:03:00"
You can use strptime to parse your objects into POSIXlt objects which, when printed, are exactly in the format you expect:
y <- strptime(x, "%Y%m%d %H:%M:%S")
# [1] "2013-06-03 00:00:03" "2013-07-03 00:01:03" "2013-08-04 00:03:03"
To remove seconds, use trunc:
y <- trunc(y, units = "mins")
# [1] "2013-06-03 00:00:00" "2013-07-03 00:01:00" "2013-08-04 00:03:00"
Having your objects as date/time objects will open a lot of doors, but if you really mean to store the output as a character vector, then just use as.character:
y <- as.character(y)
A lubridate version:
library(lubridate)
dt <- ymd_hms(x)
dt2 <- update(dt, seconds = 0)
You can try this regex, which I added a bit:
gsub("(\\d{4})(\\d{2})(\\d{2}) (\\d{2}:\\d{2}).*", "\\1-\\2-\\3 \\4:00", subject, perl=TRUE);
demo on regex101.