Questions tagged [regex-group]

Regex groups are created by placing part of a regular expression inside parentheses. Groups allows to apply a quantifier to the entire group or to restrict alternation to part of the regex. Besides grouping part of a regular expression together, parentheses also create a numbered capturing group. It stores the part of the string matched by the part of the regular expression inside the parentheses.

The regex Set(Value)? matches Set or SetValue. In the first case, the first (and only) capturing group remains empty. In the second case, the first capturing group matches Value.

If capturing the match isn't needed, the regular expression can be optimized into Set(?:Value)?. The question mark and the colon after the opening parenthesis are the syntax that creates a non-capturing group.

The question mark after the opening bracket is unrelated to the question mark at the end of the regex. The final question mark is the quantifier that makes the previous token optional. This quantifier cannot appear after an opening parenthesis, because there is nothing to be made optional at the start of a group. Therefore, there is no ambiguity between the question mark as an operator to make a token optional and the question mark as part of the syntax for non-capturing groups.

2670 questions
0
votes
0 answers

Set up a search group with regular expressions that match one or two numeric values, but do not match any more immediately following numeric values

import re input_text = "del 2065 de 42 52 de 200 de 2222 25 de 25 del 26. o del 8" #example input num_pattern = r"(\d{1,2})" identification_regex = r"(?:del|de[\s|]*el|de|)[\s|]*" + num_pattern input_text = re.sub(identification_regex, "AA",…
Matt095
  • 857
  • 3
  • 9
0
votes
1 answer

Why does capturing the capture group identified with this regex search pattern fail?

import re input_text_substring = "durante el transcurso del mes de diciembre de 2350" #example 1 #input_text_substring = "durante el transcurso del mes de diciembre del año 2350" #example 2 #input_text_substring = "durante el transcurso del mes 12…
Matt095
  • 857
  • 3
  • 9
0
votes
1 answer

Regex to allow only alphabet and special alphabets like é

I want a regex which only allows a-z A-Z and special characters like é (accented). I don't want any special characters like @,.$%^&" lastName.addEventListener( 'keyup', inputValidation.bind({ // reg: /^[A-Za-z ]+$/, reg:…
0
votes
1 answer

Set alphanumeric regex pattern not accepting certain specific symbols

import re #Examples: input_text = "Recien el 2021-10-12 despues de 3 dias 2021-10-12" #NOT PASS input_text = "Recien el 2021-10-12 hsah555sahsdhj. Ya despues de 3 dias hjsdfhjdsfhjdsf 2021-10-12" #NOT PASS input_text = "Recien el 2021-10-12…
Matt095
  • 857
  • 3
  • 9
0
votes
1 answer

how to automatically replace a tag predefined

I have some characters that need to be replaced as above but I don't know how: characters to replace: first | end | | | | | | | day => get now day (ex: 14) red => color red
0
votes
0 answers

How to perform replacement with re.sub() if and only if there is a ; or \n. in the middle of the capture groups?

import re, datetime #Ejemplos en donde si se debe hacer uno o mas reemplazos input_text = "Decian muchas cosas; Seguro eso ocurrira despues de 3 dias" input_text = "seguro eso ocurrira despues de 3 dias.\n empieza el 2021-11-12" input_text =…
Matt095
  • 857
  • 3
  • 9
0
votes
2 answers

How to capture string of characters from where it is indicated to the first point followed by a line break?

import re x = """44 5844 44554 Hi hi! , sahhashash; asakjas. jjksakjaskjas. ooooooppkkk""" #both initial after the last line break that they have within their capture range # ((?:\w+)?) ---> with a capturing group this pattern can capture a…
Matt095
  • 857
  • 3
  • 9
0
votes
0 answers

Replacement using the re.sub() if there a date indicated before or after the substring to be replaced, and if there is NO a "/n" or a ";" in between

import re, datetime #operation function def add_or_subtract_days(days, operation): today = datetime.date.today() if operation == "add" : input_text = (datetime.datetime.strptime(today, "%Y-%m-%d") +…
Matt095
  • 857
  • 3
  • 9
0
votes
0 answers

Regex for URL filtering

I have a URL which needs to be filtered from a certain logs if it has user-specific information. URL looks something like this: /v1/info/infor1/users/ABC (/v1/info/:info/users/:userID #info and userID are parameters) If I write a regex like…
Neha Ahir
  • 1
  • 1
0
votes
0 answers

Regex to repair malformed XML attributes, removing spaces in tags

I've been unfortunate to have come accross alot of malformed XML. I cannot get the correct regex to remove 2 spaces inside the attribute/key. My current regex also checks to see if there is a valid "=" attribute. XML attributes have to have a value…
RY4N
  • 1,080
  • 3
  • 14
  • 31
0
votes
0 answers

Python Regex: capture all optional groups, regardless of order

For a string "I have a dog, a fish, and a cat", I would like to capture the groups in the order "dog", "fish", and "cat". I have a Python regex that works the way I want, making the groups optional in case the string doesn't contain the groups. So…
mmille
  • 54
  • 3
0
votes
1 answer

Extract date from string in date format, add n number of days. to then replace with that modified data another substring within the original string

import re, datetime, time input_text = "tras la aparicion del objeto misterioso el 2022-12-30 visitamos ese sitio nuevamente revisando detras de los arboles pero recien tras 3 dias ese objeto aparecio de nuevo tras 2 arboles" #example 1 input_text…
Matt095
  • 857
  • 3
  • 9
0
votes
1 answer
0
votes
4 answers

regex match and replcae <..> characters

I have a requirement to match all array<..> in the entire sentence and replace only <> to [] (replace <> with [] which have prefix array). I haven't got any clue to resolve this. It will be great if anyone can provide any clue for this…
Abhishek Kumar
  • 435
  • 1
  • 6
  • 16
0
votes
2 answers

REGEX : Extracting Table information from connection strings

I am attempting to extract Schema and Table information from connection string data. The Schema and Table information is in the format "Schema.Table" (e.g FROM EDWP_D2PM.SN_INC_RPTG_SCRUBBED in string below) . Multiple Schema and Tables can exist in…
Steve
  • 475
  • 4
  • 12
  • 25