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
Related
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
I beseech thee, oh gods of the mighty RegEx... hear my plee!!!
I need a Regex for the following rules
-Number has to be 6 characters long
-Number has to start with 5
Number cannot start with "50****" or "589***"(It can start with 51, 52, 587, 583, etc...)
What I'm working off of right now is
^5(?!(0\b|89\b))\d+\b.
Please HELP!!!
^5(?!(?:0|89))\d{5}$
This should do it for you.See demo.
https://regex101.com/r/hE4jH0/1
You dont need \b after 0 or 89 as you dont expect a word boundary there.
I am trying to extract some data from the following examples:
Name 789, 10-mill 12-27b
Manufacturer XY-2822, 10-mill, 17-25b
Other Manufacturer 16b Part
Another Manufacturer FER M9000, 11-mill, 11-40
18b Part
Maker 11-31, 10-mill
Maker 1x or 2x; max size 1x (34b), 2x (38/24b)
Maker REC6 15/18/26b. Square.
Producer FC-40 11-13-16-19-22-25-27-30-34b
What I'd like my results to be respectively are:
12, 27
17, 25
16
11, 40
18
11-31
34, 38, 24 (optional, its fine if only the latter two are provided)
15, 18, 26
11, 13, 16, 19, 22, 25, 27, 30, 34
I am happy to do this in multiple passes, using an expression grammar though I don't think that'll really help.
I'm having trouble using lookaheads and lookbehinds to grab that data and exclude things like "11-mill" and "XY-2822". What I find happening is I am able to exclude those matches but end up truncating good results for others matches.
What is the best way to go about this?
My current regex is
/(?:(\d+)[b\b\/-])([b\d\b]*)[^a-z]/i
which is capturing the letter 'b' (which is okay) but not capturing 34b in the final example
Not sure what are your exact requirements/formats but you can try this:
/(?:\G(?!^)[-\/]|^(?:.*[^\d\/-])?)\K\d++(?![-\/]\D)/
http://rubular.com/r/WJqcCNe2pr
details:
# two possible starts:
(?: # next occurrences
\G # anchor for the position after the previous match
(?!^) # not at the start of the line
[-\/]
| # first occurrence
^
(?:.*[^\d\/-])? # (note the greedy quantifier here,
# to obtain the last result of the line)
)
\K # discards characters matched before from the whole match
\d++ # several digits with a possessive quantifier to forbid backtracking
(?![-\/]\D) # not followed by an hyphen of a slash and a non-digit
You can improve the pattern if you replace (?:.*[^\d\/-])? with [^-\d\/\n]*+(?>[-\d\/]+[^-\d\/\n]+)* (remove the \n if you work line by line.). The goal of this change is to limit the backtracking (that occurs atomic group by atomic group, instead of character by character for the first version).
Perhaps, you can replace the negative lookahead with this kind of positive lookahead: (?=[-\/]\d|b|$)
An other version here.
Perhaps this:
(?<=\d-)\d+|\d+(?=-\d+)|\d+(?=(?:\/\d+)*b)
https://regex101.com/r/nR3eS9/1
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]