How to add suffix in a line using regex? - regex

How to add suffix in a line using regex? This is my regex:
https://regex101.com/r/vV4zX8/1
The result should be like this:
My sample input is:
one
two
three
four
five
six
seven
So far all I've been able to come up with to use is:
\n replace with - digit \n
But I need the output of:
one - digit
two - digit
three - digit
four - digit
five - digit
six - digit
seven - digit

You can try the regex \b$ which ensures there's a match where a 'word' ends, and replace with - digit (or \b(\n|$) with a replacement of - digit$1 if you don't want to use multiline)
regex101 demo

You could also use ([^\r?\n])$

Regex:
(.+)$
or
(.)$
And don't forget to enable mulitine modifier m.
Replacement string:
\1 - digit
DEMO

You can try like this : check below image
For DEMO

Related

Regular Expression to Validate Monaco Number Plates

I would like to have an expression to validate the plates of monaco.
They are written as follows:
A123
123A
1234
I started by doing:
^[a-zA-Z0-9]{1}?[0-9]{2}?[a-zA-Z0-9]{1}$
But the case A12A which is false is possible with that.
You can use
^(?!(?:\d*[a-zA-Z]){2})[a-zA-Z\d]{4}$
See the regex demo. Details:
^ - start of string
(?!(?:\d*[a-zA-Z]){2}) - a negative lookahead that fails the match if there are two occurrences of any zero or more digits followed with two ASCII letters immediately to the right of the current location
[a-zA-Z\d]{4} - four alphanumeric chars
$ - end of string.
You can write the pattern using 3 alternatives specifying all the allowed variations for the example data:
^(?:[a-zA-Z][0-9]{3}|[0-9]{3}[a-zA-Z]|[0-9]{4})$
See a regex demo.
Note that you can omit {1} and
To not match 2 chars A-Z you can write the alternation as:
^(?:[a-zA-Z]\d{3}|\d{3}[a-zA-Z\d]|\d[a-zA-Z\d][a-zA-Z\d]\d)$
See another regex demo.
So it needs 3 connected digits and 1 letter or digit.
Then you can use this pattern :
^(?=.?[0-9]{3})[A-Za-z0-9]{4}$
The lookahead (?=.?[0-9]{3}) asserts the 3 connected digits.
Test on Regex101 here

Regex: Find last occurrence of digit pair

I'm trying to find the last match of a digit pair in some kinds of strings like these:
123f64swfW68F43
123f64swfW6843
123f64swfW6843sad
123f64swfW6843sa3d
In all of them the matching result should be 43. I tried my best and came to:
/(\d{2})(?!.*\d)/
But this works only for the first three strings.
Please note that I want to do this in one regular expression and without any further scripting.
Thanks for your help!
You may use
\d{2}(?!\d|.*\d{2})
See the regex demo. It basically means "match 2 consecutive digits that are not immediately followed with a digit and that not followed with any 2 consecutive digits anywhere to the right of those two digits".
Details
\d{2} - two digits
(?!\d|.*\d{2}) - that are not followed with a digit or with any two digits aftr any 0+ chars other than line break chars.
Alternatively, you may use
/.*(\d{2})/
and grab Group 1 value. See the regex demo. This regex means "match all text it can to the last two digits, and capture the two digits in a separate memory buffer".
Details
.* - any 0+ chars other than line break chars, as many as possible
(\d{2}) - Capturing group 1: two digits

Regex : must end with the first match of exaclty 10 digits between dashes

2017-34-5-1503650477-547-1234567890-coco.jpg
I want to match 2017-34-5-1503650477-
2017-34-5-1503650477-toast.jpg
I want to match 2017-34-5-1503650477-
2017-240-1503650477-toast.jpg
I want to match 2017-240-1503650477-
I'm trying to use /\b^(.*)\-\d{10}\-\b/ but on the first example it matches 2017-34-5-1503650477-547-1234567890- wheras I want to stop here : 2017-34-5-1503660477-
You should add the laziness modifier:
Note the question mark in (.*?)
\b^(.*?)\-\d{10}\-\b
Here is a regex101 example:
https://regex101.com/r/nQfsE9/1
You could use:
^(\d{1,9}-)+\d{10}
Where:
(\d{1,9}-)+ pairs of 1-9 digits and -
\d{10} followed by 10 digits
example

Regex for 5 digit number with optional characters

I am trying to create a regex to validate a field where the user can enter a 5 digit number with the option of adding a / followed by 3 letters. I have tried quite a few variations of the following code:
^(\d{5})+?([/]+[A-Z]{1,3})?
But I just can't seem to get what I want.
For instance l would like the user to either enter a 5 digit number such as 12345 with the option of adding a forward slash followed by any 3 letters such as 12345/WFE.
You probably want:
^\d{5}(?:/[A-Z]{3})?$
You might have to escape that forward slash depending on your regex flavor.
Explanation:
^ - start of string anchor
\d{5} - 5 digits
(?:/[A-Z]{3}) - non-capturing group consisting of a literal / followed by 3 uppercase letters (depending on your needs you could consider making this a capturing group by removing the ?:).
? - 0 or 1 of what precedes (in this case that's the non-capturing group directly above).
$ - end of string anchor
All in all, the regex looks like this:
You can use this regex
/^\d{5}(?:\/[a-zA-Z]{3})?$/
^\d{5}(?:/[A-Z]{3})?$
Here it is in practice (this is a great site to test your regexes):
http://regexr.com?36h9m
^(\d{5})(\/[A-Z]{3})?
Tested in rubular

Regex to match "[any single digit number]" and ":any four digit number/"

I require the regex to match the following pattern in vbscript
[any single digit number] and :any four digit number/
That is, a open square bracket followed by a single digit number followed by a close square bracket or a colon followed by four digit number followed by a slash
I've tried the following pattern which select just a single digit also
[\[]*([:]*([0-9]{1,4})[/]*)[]]*
Thanks in advance,
Madhan
Try like this:
(\[\d\]|\:\d{4}\/)
Live Demo
This should work:
(\[\d\]|:\d{4}/)