Use of Gsub in editing a column - regex

I am trying to replace text data in a column of the form: Defoe 12, 69, Johnson 40 with Defoe 12, Defoe 69, Johnson 40 with -
latest$AGS24 <-gsub(pattern="(.*)([[:space:]])([[:digit:]]),([[:space:]])([[:digit:]]),([[:space:]])",replacement="\\1\\3\\1\\5",latest$AGS23)
but this is not doing the job. Any help will be much appreciated.

You regex doesn't work because [[:digit:]] indicates a single digit but not multiple ones.
You can use this approach:
test <- "Defoe 12, 69, Johnson 40"
gsub("\\b(\\w+\\s)(\\d+,\\s)(?=\\d)", "\\1\\2\\1", test, perl = TRUE)
# [1] "Defoe 12, Defoe 69, Johnson 40"

Related

Visual studio code - regex - edit multiple line?

I want to use visual studio code regex to introduce commas and single quote at different point of a line. Can you help please?
I want to transform
(1 ant 18 0 test abacus123 789 pass),
(2 dog 26 67 exp b+45 456 fail),
(3 tiger 5 2 'reg e-t' 126 fail),
To
(1, 'ant', 18, 0, 'test abacus123', 789, 'pass'),
(2, 'dog', 26, 67, 'exp b+45', 456, 'fail'),
(3, 'tiger', 5, 2, 'reg e-t', 126, 'fail'),
There are so many lines of data that i have to transform like this, not sure how to do it.
Any help is much appreciated.
You need to provide more examples or describe the rules better.
According to the examples you have provided so far, you may try the following regex:
Regex
\((\d+) '?(.+?)'? (\d+) (\d+) '?(.+?)'? (\d+) '?(\w+)'?\)
Substitution
($1, '$2', $3, $4, '$5', $6, '$7')
Check the test cases

Regular expression for matching a list of numbers [duplicate]

This question already has answers here:
Regular expression for exact match of a string
(6 answers)
Closed 6 years ago.
I have a list of valid numbes which looks like this:
50, 56, 62, 68, 74, 80, 86, 92, 98, 104, 110, 116, 122, 128, 134, 140, 146, 152, 158
I need an regular expression which allows every number in this list, but nothing else. This regex is used to validate a HTML5 input control. A few examples for clarification:
50 => true
150 => false
abc => false
51 => false
110 => true
11 => false
50, 56 => false
I have tried this expression, but the problem is that numbers like 156 and 150 also get matched.
50|56|62|68|74|80|86|92|98|104|110|116|122|128|134|140|146|152|158
Can anyone help me out?
Add ^ and $ to match begining and ending of string
^(50|56|62|68|74|80|86|92|98|104|110|116|122|128|134|140|146|152|158)$
Edit
OP has mentioned in the comment that he tested his pattern using online tool and got wrong result. Also Wiktor Stribiżew mentioned in the comment that OP's pattern is correct if that pattern is used in pattern attribute of HTML 5 Input element.

Repeat groups in regex replace

I have a list of n numbers/words in each line, ex:
12
20
24
25
26
30
31
32
39
98
103
105
106
108
116
117
122
132
138
140
etc...
I want with a single regex put them in groups of n in each line, ex (n = 10):
12, 20, 24, 25, 26, 30, 31, 32, 39, 98,
103, 105, 106, 108, 116, 117, 122, 132, 138, 140,
etc...
I currently can do it in two replaces:
Match: /(\w+)\n/ -> Replace with: "$1, "
Match: /((\w+, ){10})/ -> Replace with: "$1\n"
But how to do it in one?
EDIT: No code allowed, only regex (match regex and replacement regex). If it matters, I'll be running this on sublime text 3.
I´m not sayng is pretty...
match: (\w+)\n(?:(\w+)\n)?(?:(\w+)\n)?(?:(\w+)\n)?(?:(\w+)\n)?(?:(\w+)\n)?(?:(\w+)\n)?(?:(\w+)\n)?(?:(\w+)\n)?(\w+)?
replace with: $1,$2,$3,$4,$5,$6,$7,$8,$9,$10
I have no idea how to capture the 10 groups in (\w+){10}

Regex digits smaller then 82

I'm trying to write a simple regex but I don't know why it is not working.
User enter 2 digits number like 01, 09, 23, 55, until 82. After 82 system will refuse.
Here is my regex, 2 digits must be smaller than 82.
0[1-9]|[1-8][0-9]|8[0-2]
You should have [1-7] for the range 10-79, not [1-8]. Don't forget the ^ and $ to specify the start and ending of the string:
^(0[1-9]|[1-7]\d|8[0-2])$
Why not cast to an integer and then just test x < 82?
Your second part is wrong. It'll match from 10 to 89, whereas you want it to match from 10 to 79 and let the third part handle 80 to 82.
0[1-9]|[1-7][0-9]|8[0-2]

Regex to Add a Character in a Space Pattern

I have something like-
[[59],
[73 41],
[52 40 09],
[26 53 06 34],
[10 51 87 86 81],
[61 95 66 57 25 68]]
I need to add a comma before every space to be like -
[[59],
[73, 41],
[52, 40, 09],
[26, 53, 06, 34],
[10, 51, 87, 86, 81],
[61, 95, 66, 57, 25, 68]]
What would be regex string for that?
Judging from your data, you may just replace a space ' ' by a comma followed by a space ', '. You do not need a regex for that.
This depends on what regex flavor you are using but in general, looking for matches would be
(\d+)\s
and replacing would be
\1,
In Notepad++, open up the find control window with Ctrl+H.
In Find What put a single space character
In Replace With put a comma followed by a space character
This gives the expected output, but isn't very interesting as far as Regexes go.
s/\( \)/,\1/g
And, as an afterthought:
s/ /, /g
Why bother with substitution replacement? :)
Replace (\d)\s(\d) with
\1, \2