Replaced "[0-9]" with \d in the regular expressions.

This commit is contained in:
依瑪貓 2022-12-05 19:21:37 +08:00
parent 1967359142
commit 56a9d565c1
8 changed files with 33 additions and 33 deletions

View File

@ -40,9 +40,9 @@ class TransactionTypeConverter:
class PeriodConverter:
"""The path converter for the period."""
regex = ("([0-9]{4}(-[0-9]{2}(-[0-9]{2})?)?)|"
"([0-9]{4}(-[0-9]{2}(-[0-9]{2})?)?)?-"
"([0-9]{4}(-[0-9]{2}(-[0-9]{2})?)?)?")
regex = (r"([0-9]{4}(-[0-9]{2}(-[0-9]{2})?)?)|"
r"([0-9]{4}(-[0-9]{2}(-[0-9]{2})?)?)?-"
r"([0-9]{4}(-[0-9]{2}(-[0-9]{2})?)?)?")
def to_python(self, value):
"""Returns the period by the period specification.
@ -90,7 +90,7 @@ class DateConverter:
Returns:
datetime.date: The date.
"""
m = re.match("^([0-9]{4})-([0-9]{2})-([0-9]{2})$", value)
m = re.match(r"^(\d{4})-(\d{2})-(\d{2})$", value)
year = int(m.group(1))
month = int(m.group(2))
day = int(m.group(3))

View File

@ -205,8 +205,8 @@ class TransactionForm(forms.Form):
by_rec_id = {}
for key in args[0].keys():
m = re.match(
("^((debit|credit)-([1-9][0-9]*))-"
"(id|ord|account|summary|amount)$"),
(r"^((debit|credit)-([1-9]\d*))-"
r"(id|ord|account|summary|amount)$"),
key)
if m is None:
continue
@ -271,7 +271,7 @@ class TransactionForm(forms.Form):
}
for key in post.keys():
m = re.match(
"^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)",
r"^(debit|credit)-([1-9]\d*)-(id|ord|account|summary|amount)",
key)
if m is None:
continue
@ -303,7 +303,7 @@ class TransactionForm(forms.Form):
= post[F"{record_type}-{old_no}-{attr}"]
# Purges the old form and fills it with the new form
for x in [x for x in post.keys() if re.match(
"^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)",
r"^(debit|credit)-([1-9]\d*)-(id|ord|account|summary|amount)",
x)]:
del post[x]
for key in new_post.keys():
@ -475,7 +475,7 @@ class TransactionSortForm(forms.Form):
key = F"transaction-{txn.pk}-ord"
if key not in post:
post_orders.append(form.Order(txn, 9999))
elif not re.match("^[0-9]+$", post[key]):
elif not re.match(r"^\d+$", post[key]):
post_orders.append(form.Order(txn, 9999))
else:
post_orders.append(form.Order(txn, int(post[key])))

View File

@ -216,7 +216,7 @@ class Transaction(DirtyFieldsMixin, StampedModel, RandomPkModel):
txn_type: The transaction type.
"""
self.old_date = self.date
m = re.match("^([0-9]{4})-([0-9]{2})-([0-9]{2})$", post["date"])
m = re.match(r"^(\d{4})-(\d{2})-(\d{2})$", post["date"])
self.date = datetime.date(
int(m.group(1)),
int(m.group(2)),
@ -282,8 +282,8 @@ class Transaction(DirtyFieldsMixin, StampedModel, RandomPkModel):
max_no["credit"] = 0
for key in post.keys():
m = re.match(
("^(debit|credit)-([1-9][0-9]*)-"
"(id|ord|account|summary|amount)$"),
(r"^(debit|credit)-([1-9]\d*)-"
r"(id|ord|account|summary|amount)$"),
key)
if m is None:
continue

View File

@ -329,7 +329,7 @@ function parseSummaryForCategoryHelpers(summary) {
});
// A bus route
const matchBus = summary.match(/^([^—]+)—([^—]+)—([^—]+)→([^—]+?)(?:×[0-9]+)?$/);
const matchBus = summary.match(/^([^—]+)—([^—]+)—([^—]+)→([^—]+?)(?:×\d+)?$/);
if (matchBus !== null) {
$("#summary-bus-category").get(0).value = matchBus[1];
setSummaryBusCategoryButtons(matchBus[1]);
@ -342,7 +342,7 @@ function parseSummaryForCategoryHelpers(summary) {
}
// A general travel route
const matchTravel = summary.match(/^([^—]+)—([^—]+)([→|↔])([^—]+?)(?:×[0-9]+)?$/);
const matchTravel = summary.match(/^([^—]+)—([^—]+)([→|↔])([^—]+?)(?:×\d+)?$/);
if (matchTravel !== null) {
$("#summary-travel-category").get(0).value = matchTravel[1];
setSummaryTravelCategoryButtons(matchTravel[1]);
@ -357,7 +357,7 @@ function parseSummaryForCategoryHelpers(summary) {
// A general category
const generalCategoryTab = $("#summary-tab-category");
const matchCategory = summary.match(/^([^—]+)—.+(?:×[0-9]+)?$/);
const matchCategory = summary.match(/^([^—]+)—.+(?:×\d+)?$/);
if (matchCategory !== null) {
$("#summary-general-category").get(0).value = matchCategory[1];
setSummaryGeneralCategoryButtons(matchCategory[1]);

View File

@ -184,8 +184,8 @@ function updateTotalAmount(element) {
}
});
total = String(total);
while (total.match(/^[1-9][0-9]*[0-9]{3}/)) {
total = total.replace(/^([1-9][0-9]*)([0-9]{3})/, "$1,$2");
while (total.match(/^[1-9]\d*\d{3}/)) {
total = total.replace(/^([1-9]\d*)(\d{3})/, "$1,$2");
}
$("#" + type + "-total").text(total);
}

View File

@ -42,7 +42,7 @@ def _strip_decimal_zeros(value: Decimal) -> str:
str: The value with excess decimal zeros stripped.
"""
s = str(value)
s = re.sub(r"^(.*\.[0-9]*?)0+$", r"\1", s)
s = re.sub(r"^(.*\.\d*?)0+$", r"\1", s)
s = re.sub(r"^(.*)\.$", r"\1", s)
return s
@ -58,7 +58,7 @@ def _format_positive_amount(value: Decimal) -> str:
"""
s = _strip_decimal_zeros(value)
while True:
m = re.match("^([1-9][0-9]*)([0-9]{3}.*)", s)
m = re.match(r"^([1-9]\d*)(\d{3}.*)", s)
if m is None:
break
s = m.group(1) + "," + m.group(2)

View File

@ -804,9 +804,9 @@ class SearchListView(TemplateView):
| Q(code=term)))\
| Q(summary__icontains=term)\
| Q(transaction__notes__icontains=term)
if re.match("^[0-9]+(?:\\.[0-9]+)?$", term):
if re.match(r"^\d+(?:\.\d+)?$", term):
conditions = conditions | Q(amount=Decimal(term))
if re.match("^[1-9][0-8]{9}$", term):
if re.match(r"^[1-9][0-8]{9}$", term):
conditions = conditions\
| Q(pk=int(term))\
| Q(transaction__pk=int(term))\

View File

@ -195,7 +195,7 @@ class Period:
"""
if self._data_start is None:
return None
m = re.match("^[0-9]{4}-[0-2]{2}", self._period.spec)
m = re.match(r"^\d{4}-\d{2}", self._period.spec)
if m is None:
return None
if self._period.end < self._data_start:
@ -379,9 +379,9 @@ class Period:
if self.start <= self._data_start:
return None
previous_day = self.start - datetime.timedelta(days=1)
if re.match("^[0-9]{4}$", self.spec):
if re.match(r"^\d{4}$", self.spec):
return "-" + str(previous_day.year)
if re.match("^[0-9]{4}-[0-9]{2}$", self.spec):
if re.match(r"^\d{4}-\d{2}$", self.spec):
return dateformat.format(previous_day, "-Y-m")
return dateformat.format(previous_day, "-Y-m-d")
@ -441,7 +441,7 @@ class Period:
return
self.spec = spec
# A specific month
m = re.match("^([0-9]{4})-([0-9]{2})$", spec)
m = re.match(r"^(\d{4})-(\d{2})$", spec)
if m is not None:
year = int(m.group(1))
month = int(m.group(2))
@ -452,7 +452,7 @@ class Period:
self.prep_desc = gettext("In %s") % self.description
return
# From a specific month
m = re.match("^([0-9]{4})-([0-9]{2})-$", spec)
m = re.match(r"^(\d{4})-(\d{2})-$", spec)
if m is not None:
year = int(m.group(1))
month = int(m.group(2))
@ -464,7 +464,7 @@ class Period:
self.prep_desc = self.description
return
# Until a specific month
m = re.match("^-([0-9]{4})-([0-9]{2})$", spec)
m = re.match(r"^-(\d{4})-(\d{2})$", spec)
if m is not None:
year = int(m.group(1))
month = int(m.group(2))
@ -477,7 +477,7 @@ class Period:
self.prep_desc = self.description
return
# A specific year
m = re.match("^([0-9]{4})$", spec)
m = re.match(r"^(\d{4})$", spec)
if m is not None:
year = int(m.group(1))
# Raises ValueError
@ -487,7 +487,7 @@ class Period:
self.prep_desc = gettext("In %s") % self.description
return
# Until a specific year
m = re.match("^-([0-9]{4})$", spec)
m = re.match(r"^-(\d{4})$", spec)
if m is not None:
year = int(m.group(1))
# Raises ValueError
@ -505,7 +505,7 @@ class Period:
self.prep_desc = gettext("In %s") % self.description
return
# A specific date
m = re.match("^([0-9]{4})-([0-9]{2})-([0-9]{2})$",
m = re.match(r"^(\d{4})-(\d{2})-(\d{2})$",
spec)
if m is not None:
# Raises ValueError
@ -518,8 +518,8 @@ class Period:
self.prep_desc = gettext("In %s") % self.description
return
# A specific date period
m = re.match(("^([0-9]{4})-([0-9]{2})-([0-9]{2})"
"-([0-9]{4})-([0-9]{2})-([0-9]{2})$"),
m = re.match((r"^(\d{4})-(\d{2})-(\d{2})"
r"-(\d{4})-(\d{2})-(\d{2})$"),
spec)
if m is not None:
# Raises ValueError
@ -564,7 +564,7 @@ class Period:
self.prep_desc = gettext("In %s") % self.description
return
# Until a specific day
m = re.match("^-([0-9]{4})-([0-9]{2})-([0-9]{2})$", spec)
m = re.match(r"^-(\d{4})-(\d{2})-(\d{2})$", spec)
if m is not None:
# Raises ValueError
self.end = datetime.date(