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}
Related
I am trying to build a regex for following rules
No characters – only numbers are allowed to enter
Numbers must contain not less than 8 digits
Numbers and combinations, starting as follows should not be allowed:
[3.1] - Any number starting with 0 (zero) and with 1 (one);
[3.2] - Numbers starting with 20, 21, 22, 23, 24, 25, 26, and 27.
I am able to achieve regex for points 1, 2 and 3.1 like this
^[2-9]{1}[0-9]{7,}$
But I am not able to find solution for point 3.2
This is one of the options I started with, this regex matches the string that must start with 20, 21, 22, 23, 24, 25, 26, or 27.
^([2][0-7])[2-9]{1}[0-9]{7,}$
I just have to find the negation of this first condition.
Please help!
Rather than trying to negate the 20-27 condition, just match numbers that start with 28 or 29 instead:
^(?:2[89]|[3-9]\d)\d{6,}$
Demo on regex101
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.
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"
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]
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