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.
Related
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
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
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 8 years ago.
Improve this question
The simulation code I use needs to read parameters from an input.txt file which looks like
paramA = 1,paramB = 2,
paramC = 3,paramD = 4,
When scanning a parameter (like paramC) in my simulation, I need to change the value of paramC every time manually. How can I do this with a perl script so that when I type
perl scriptname input.txt paramC 100
in the command line I can get a modified input file with paramC changed to 100
paramA = 1,paramB = 2,
paramC = 100,paramD = 4,
I can do this by creating a template file like
paramA = 1,paramB = 2,
paramC = <>,paramD = 4,
and then use perl to match the mark <> and replace it with the value I want. However is there a more direct way to match the parameter name and change its value ?
thanks.
The obvious answer is to use a regular expression. Perl is quite good at those.
So you could - for example - do:
s/paramC = \d+/paramC = $value/g;
Which'll do the trick I'd have thought?
Edit: Or use TLP's pattern in the comments:
s/^paramC = \K[^ ,]+/$value/g;
or perhaps:
s/^paramC = \K\d+/$value/g;
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.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Hannuka, Chanukah, Hanukkah...Due to transliteration from another language and character set, there are many ways to spell the name of this holiday. How many legitimate spellings can you come up with?
Now, write a regular expression that will recognise all of them.
According to http://www.holidays.net/chanukah/spelling.htm, it can be spelled any of the following ways:
Chanuka
Chanukah
Chanukkah
Channukah
Hanukah
Hannukah
Hanukkah
Hanuka
Hanukka
Hanaka
Haneka
Hanika
Khanukkah
Here is my regex that matches all of them:
/(Ch|H|Kh)ann?[aeiu]kk?ah?/
Edit: Or this, without branches:
/[CHK]h?ann?[aeiu]kk?ah?/
Call me a sucker for readability.
In Python:
def find_hanukkah(s):
import re
spellings = ['hannukah', 'channukah', 'hanukkah'] # etc...
for m in re.finditer('|'.join(spellings), s, re.I):
print m.group()
find_hanukkah("Hannukah Channukah, Hanukkah")
Something like C?hann?uk?kah? matches most of the common cases. There also a bunch of weirder spellings C?hann?uk?kah?|Han[aei]ka|Khanukkah matches almost every spelling I could think of (that had at least half a million hits on google).
((Ch|H|X|Х|Kh|J)[aа](н|n{1,2})(у|ou|[auei])(к|k|q){1,2}[aа]h?)|(חנו?כה)
This regex is much more inclusive and covers all of the following options:
Channuka
Channukah
Channukka
Channukkah
Chanuka
Chanukah
Chanukah
Chanukka
Chanukkah
Chanuqa
Hanaka
Haneka
Hanika
Hannuka
Hannukah
Hannukka
Hannukkah
Hanoukka
Hanuka
Hanukah
Hanukka
Hanukkah
Januka
Khanukkah
Xanuka
Ханука
Ханука
חנוכה
חנכה
Try this:
/^[ck]?hann?ukk?ah?$/i
I think the only approved spellings in English are Hanukkah and Chanukh, so it's something like
/(Ch|H)anuk?kah/
Or maybe even better
/(Chanukah|Hanukkah)/
I like Triptych's answer, but i would take it one step forward... also in python:
def valid(spelling):
import re
regex_spelling = re.compile(r'^[cCkK]{0,1}han{1,2}uk{1,2}ah$')
valid = regex_spelling.match(spelling)
if valid:
print 'Valid spelling'
else:
print spelling, " is not a spelling for the word"
to use it:
valid("hanukkah")