How to replace “35yrs” with “35 yrs” using regular expressions? - regex

This question is about Search & Replace.
I have a list that looks like this:
35yrs
74 yrs
40yrs
24yrs
36 yrs
I want to use regular expressions to make the list look like this:
35 yrs
74 yrs
40 yrs
24 yrs
36 yrs
I have this for the search:
\d+[y][r][s]
What should the replace string look like? Textmate's search engine requires numbers in the result, e.g., $1, that represent the regex in the search field.

Capture with:
*(yrs)
and replace with:
\1
Note the leading whitespace in both match and replacement. Demo here.

There’s no reason for […] around individual letters — ever.
For your expression, just capture the digits:
(\d+)\s?yrs
And replace them:
\1 yrs
Strictly speaking matching the space (\s?) is unnecessary: if you do not include it, those entries that already include a space will not be matched, which is fine: theyʼre already correct, after all.

You could use the following regex:
(\d+)\s*(yrs)
And replace it with:
$1 $2
This matches the number in the first capturing group (indicated with ()) and the yrs in the second. The replacement is then both capturing groups with a space between. The whitespace is not matched in between the two capturing groups and is optional.

If you're using Java, you can do it like so.
String str = "35yrs";
str = str.replaceAll("(\\d+)\\s*yrs)", "$1 yrs);
Changes years followed by zero or more spaces and yrs to years followed by one space followed by yrs.

Related

Regex extracting specific text ignoring part of result

I have a text.
The overall system blur must be smaller than 12 mrad and diameter shall be less than 20 meters.
I want to extract:
must be 12
I'm using:
^(.*?(\bmust be\b[\s*?\D]*\d+)[^$]*)$
And I get
must be smaller than 12
Any way to do this directly? Or better to try to do different groups somehow?
In the pattern that you tried, \D matches any char except a digit, so this [\s*?\D]* could be shortened to just \D.
This part [^$]* matches any char except a dollar sign. If you intend to match the rest of the line, you can use .* instead.
You can use 2 capturing groups instead.
^.*?\b(must be)\b\D*(\d+).*$
Regex demo
In the replacement use the 2 capturing groups like for example $1 $2

Notepad ++ Regular Expressions

Notepad ++ Replacing Multiple Words
Okay so heres what i need to know, currently i am searching multiple words at once, heres some sample data
(\bACCESS\b)|(\bAccs\b)|(\bALLEY\b)|(\bAlly\b)|(\bALLEYWAY\b)
What i want to do is add a ":" to the end of every word that is found. Like this
41 dwadadad Rd:
93 awdawdadawd Terrace:
4/100 awdadawdwad St:
32 awdawdawdawd Ave:
59 awdawdawd Street: Ferny Grove
Is there a regular expression for only getting the end of the matched word?
I suggest using an alternation list with just two word boundaries - at the start and end of the pattern, and just one group:
\b(?:Rd|Terrace|St|Ave|Street)\b
And replace with $0: (where $0 backreference references the whole match, if the pattern matched Rd, the Rd will be inserted in the resulting string).
Note that we can use 2 \b only becayse they enclose the alternation non-capturing group (?:...), and are thus applied to each alternative. It shortens the regex and speeds it up.
All you have to do is change your regex to:
((\bACCESS\b)|(\bAccs\b)|(\bALLEY\b)|(\bAlly\b)|(\bALLEYWAY\b))
And then replace with: \1:

Regexp: find out if value that repeats several times

I have strings:
TH 8H 5C QS TC
9S 4S JS KS JS
I want the second one to be picked up by reqexp. Help me please to contract the necessary expression.
What I tried so far is: S{5} but of course it look up sequentially.
Could I avoid determining which character I am looking for. I need 5 repetition of any. Could it be like .{5} ?
Thanks in advance!
If you have standalone strings, use
^\wS(?: \wS){4}$
See the regex demo
If these strings appear inside a larger text, replace the ^ and $ anchors with word boundaries \b:
\b\wS(?: \wS){4}\b
See another demo
Note that \w matches any alphanumeric or underscore character. If there can be any non-whitespace character, use \S instead:
\b\SS(?: \SS){4}\b
One more demo
\SS will match a non-whitespace followed with an S and (?: \SS){4} will match 4 same sequences (thus, there will be 5 2-character sequences with S at the end of each).

Capturing repeated word sequence

In Perl, to match text pattern like a11a, g22g, x33x below regex works fine
([a-z])(\d)\g2\g1
Now i want to match repeating groups like similar to above but having space in between words like
abcd 101 abcd 101 ( catch this entire string in single regex pattern in one single line text or a paragraph )
How to do this...i tried below pattern but it wont work
([a-zA-Z]*\s)([0-9]*\s)\g1\g2
#logic is : words followed by space in 1 group and
#numbers followed by space in 2nd group
Regex101 Demo
Also, please explain why the above regex fails to capture the desired text pattern!!!
EDIT
One more complication :
assume that pattern is something like
[words][space][numbers][space][words][space][numbers]
#assume all [numbers] and [word] are same
....so in last [numbers] case, [space] doesn't follow, how to filter then...because regex group capture like:
([0-9]*\s) certainly fails to capture last part if it is repeated, and
([0-9]*) would fail to capture mid-part if it is repeated!! ??
Regex 101
Your problem is that your regex expects a space at the end, because you have included the space in the captures.
Try instead:
([a-zA-Z]+)\s([0-9]+)\s\g1\s\g2
([0-9]*\s) = 101 with space
so \g2 doesn't match with 101 as it doesn't have any space at the end.
Update: Working regex ([a-zA-Z]*\s)([0-9]*)\s\g1\g2 for input abcd 101 abcd 101
Online Demo
More example:
([a-zA-Z]*\s) ([0-9]*) \s \g1 \g2
abcd+space 101 Space abcd+space 101

Notepad++ replace all using regular expression

I have lines of numbers
16
18
19
21
24
25
26
30
How can I put commas at the end of each number using regular expressions. For example: 16 will turn to 16, and 18 will turn to 18, and so on
The question is not completely clear to me.
1. Only digits in a row and nothing else
Then Bohemians answer is working.
^(\d+)$
and replace with \1,.
The ^ anchors the sequence of digits to the start of the row and the $ to the end.
2. The digits can be anywhere in the row together with other stuff
Then tafoo85 answer is working:
(\d+) and replace with \1,.
But this will replace also "tafoo85" with "tafoo85," and "2fast4you" with "2,fast4,you"
To avoid this behaviour and matching only "standalone" numbers, you would have to use word boundaries but those are not available in Notepad++.
Because Notepad++ regexes are very limited you would have to workaround this issue in four steps:
^(\d+)$and replace with \1,
^(\d+)(\s) and replace with \1,\2
(\s)(\d+)(\s) and replace with \1\2,\3
(\s)(\d+)$ and replace with \1\2,
3. Change only digits at the start of the row
use only the start of the row anchor ^
^(\d+) and replace with \1,.
Find: ([0-9]+)
Replace with \1,
Find: (^[0-9]+$) (means the whole line is all digits - and capture it)
Replace: \1, (means the first captured group then a comma