Regex to capture and reposition the same pattern - regex

I have a list of numbers that I would like to reformat, but I'm having difficulty with (I think) the substitution -- I'm capturing the groups as I intend to, but they aren't being rendered the way I expect them to be.
Here's some of the text:
Rear seal:
102
111
112
113
137
156
And the expected output is this:
Rear seal:
102 111 112
113 137 156
I'm using this regex to distinguish the first, second, and third lines:
(\d{3}[\n\r])(\d{3}[\n\r])(\d{3}[\n\r]) coupled with \1\t\2\t\3\n for the substitution. But for some reason it comes out as
Rear seal:
102
111
112
113
137
156
I'm using the excellent site regex101.com for testing, but I could use some human input. Specific link is
https://regex101.com/r/R7niEU/1 for this issue.
Thanks in advance.

You are capturing the newline in the capturing group. That way it will also be part of the replacement.
You can only capture the digits and match the newline instead.
Then replace with \1\t\2\t\3\n
(\d{3})[\n\r](\d{3})[\n\r](\d{3})[\n\r]
Regex demo

Related

Regex - Extract string between characters if they exist

I would need to use RegEx to extract a string between characters if they exist (The colon character).
Examples:
SX: 22AA 001 267
2294 0BB 267: 09
2294 0CC 267
In all cases, I want the result.
2294 001 267
Thank you all.
You can use this regex to match them all
(?:^|:)\s?([A-Z\d]+(?: [A-Z\d]+)+)(?:$|:)
NOTE: As you did not mention what language you're using I decided to not use lookarounds. So you have to get the first group from the match.

How do I format a list of phone numbers using regular expression in vim commands?

Given the following list of phone numbers
8144658695
812 673 5748
812 453 6783
812-348-7584
(617) 536 6584
834-674-8595
Write a single regular expression (use vim on loki) to reformat the numbers so they look like this
814 465 8695
812 673 5748
812 453 6783
812 348 7584
617 536 6584
834 674 8595
I am using the search and replace command. My regular expression using back referencing:
:%s/\(\d\d\d\)\(\d\d\d\)\(\d\d\d\d\)/\1 \2 \3\g
only formats the first line.
Any ideas?
Try this:
:%s,.*\(\d\d\d\).*\(\d\d\d\).*\(\d\d\d\d\).*,\1 \2 \3,
First use count to match a pattern multiple times, it is a bad habbit to repeat the pattern:
\d\{3} "instead of \d\d\d
Than you also have to match the whitespaces etc:
:%s/.*\(\d\{3}\).*\(\d\{3}\).*\(\d\{4}\).*/\1 \2 \3/g
Or even better, escape the whole regex with \v:
:%s/\v.*(\d{3}).*(\d{3}).*(\d{4}).*/\1 \2 \3/g
This greatly increases readability

phone number RegEx not working for some strings

I want to recognize phone number as 9 consecutive figures which can be separated by white spaces, non-breaking spaces etc. with regEx "(\s*\d\s*){9}"
I run VBA macro (JS RegEx) and here are example strings which work fine with above RegEx:
ul. 27 Grudnia 16, tel. 21 287 31 61, fax 61 286 69 60 –
ul. Wrzosowa 110/120/222, kom. 692 601 428
And here is an example where phone number is not detected in VBA, but is detected by RegEx JS online tools:
al. Mazowieckiego 63, kom. 622 769 694 –
Strings which are detected and these which are not, have the same structure, so I have no idea why VBA doesn't detect phone number in some of them.
It came out that VBA changed some strings to look in - replaced a whitespace - chr(32) with a non breaking chr(160).
Removing chr(160) from string to look in solves the problem.
Also I will try to find RegEx which will let non-breaking spaces, because \s* doesn't do so, at least in VBA.

regex - if the first digit is 1 return 1 but if it is 145 return 145 but if its 133 return 133

here is my regex demo
as the question states:
if the first digit is 1 return 1 but if it is 145 return 145 but if its 133 return 133
sample dataa:
K'8134567
K'81345678
K'6134516789
K'61345678
K'643456
K'646345678
K'1234567890
K'12345678901
K'1454567890 <<<--- want 145 returned and not 1
K'13345678901 <<<--- want 133 returned and not 1
K'3214567890123
K'32134567890123
K'3654567890123
K'8934567890123
K'6554567890123
regex exprtession:
K'(?|(?P<name1>81)\d+|(61)\d+|(64)\d+|(1)\d+|(44)\d+|(86)\d+|(678)\d+|(41)\d+|(49)\d+|(33)\d+|(685)\d+|(\d{1,3})\d+)
the regex explained:
I am interested in the digits after K'
I am looking to do this using regex but not sure if it can be done.
What I want is:
if the number starts with 81 return 81
if the number starts with 61 return 61
...
if the number starts with something i am not interested in return other(or its first digits of 1-3)
The above criteria works:
but my question is how do I do the following:
if the fist digit is 1 then return 1 BUT
if the fist digit is 1 and the 2nd and 3rd digit are 45 return 145 and don't return just 1
if the fist digit is 1 and the 2nd and 3rd digit are 33 return 133 and don't return just 1
I presume I have to put something inside this part of the regex |(1)\d+|
Som other questions for my own reference:
Does regex sort the data first?
Is the order of the regex search important to how it is implemented? i deally I do not want this.
You can use this regex:
K'(?P<name1>81|61|64|44|86|678|41|49|33|685|1(?:33|45)?|\d{2,3})\d+
Updated RegEx Demo
Try with:
K'(?|(?P<name1>81)\d+|(61)\d+|(64)\d+|(1(?:45|33)?)\d+|(44)\d+|(86)\d+|(678)\d+|(41)\d+|(49)\d+|(33)\d+|(685)\d+|(\d{1,3})\d+)
DEMO
regex doesn't sorts anything but the order of your regex is important, actually based on your regex engine it would be a bit different but since most of regex engines use Traditional NFA for parsing string the order is important.
And in this case you can simply us following regex or add it to your regex :
(?<=K')1(?:45|33)?
See demo https://regex101.com/r/rT2yJ0/1

Writing a Regular Expression

I am trying to write one regular express to search for a phone number similar to
011 (134) 1234567892.
The country code must only be 011. And the area code in () can be 134 132 131 138 136 or 137. The last 10 numbers can be random. I have this
((\<011[\-\. ])?(\(|\<)\d\d\d[\)\.\-/]?)?\<\d\d\d\d\d\d\d\d\d\d\>
but it is only giving me one result.
If any could please give me some help..that would be great! Thanks.
This one should work:
(011 \(13[124678]\) \d{10})
You can see working DEMO which shows couple of correct and incorrect inputs.
^011 \(13[124678]\) \d{10}$
seems to match all of the phone numbers I tried given your constraints
^ matches the start of string
011 matches only 011
\(13[124678]\) matches 134 132 131 138 136 or 137
\d{10} matches a digit using the digit character class exactly 10 times using the repeat N syntax {n}
/011 \(13[124678]\) \d{10}/g
Don't forget the g flag to match all the occurrences.