Match string with prefix and at least one comma (,) - regex

Trying to match a comma seperated list of values.
I want to check that a comma ',' occurs at least once and string contains a certain prefix
ie
tel_local: 123456, tel_national: 123456
is valid but:
tel_local: 123456 is not as no comma
Currently using
^(tel_local:)|(tel_national:),+$
but it matches tel_local: 123456

Just try with:
^(\w+: \d+)(?:, (\w+: \d+))*$

To do exactly what you ask, use:
^tel_(?:local|national):(?=.*?,)
Demo
First, your main problem was the alternation. Think about if you have a conditional (A=B OR A=C) AND Z=5. The parenthesis are necessary in order for the order of evaluating to remain correct, same with the alternation ((?:local|national)) in your expression.
Then, since you didn't specify what had to come after tel_local or tel_national (one would assume a space and digits) asides from that it needed a comma, I used a lookahead assertion. (?=.*?,) will look ahead 0+ characters and check for a ,. If no comma is found, the assertion will fail and your match will end.

You can use this lookahead based regex:
^(?=[^,]*,)((?:tel_local|tel_national): *\d+,? *)+$
RegEx Demo

you need to include the kleene plus ("+") in a regular expression to state that some term should feature at least one or more times
therefore, if you want to specify a string to match should contain at least one comma something like ",+" should do

Related

Regex to allow only numbers and only one comma (not prefix or postfix comma)

I am using regex method to check that string contains only numbers and just one comma and accept (not prefix or postfix)
EX-> 123,456 Accepted
EX-> ,123456 NOT accepted
EX-> 123456, NOT accepted
I am using below regex but it does not work, it allows to repeat commas and only works for prefix commas
[0-9]+(,[0-9]+)*
Can anyone help me?
You can use this pattern to test a full string:
^[0-9]+,?[0-9]*\b$
Here, the comma is optional, but since the "last" digits are optional too, I added a word boundary to ensure that the comma doesn't end the string. (a word boundary fails between a comma and the end of the string.)
To allow a single comma and not match when surrounded a comma, you could make use of a word boundary and lookarounds (if supported) to assert not a comma to the left and the right.
(?<!,)\b[0-9]+(?:,[0-9]+)?\b(?!,)
Regex demo
If the numbers are the only string, you can use anchors to assert the start and the end of the string.
^\d+(?:\,\d+)?$
Regex demo
try this : \b\d+,\d+\b
with this expression all the matches will contain only one coma but the coma is not optional in this case.

Regex expression for [number2] in [number],[number2][word]

I'm trying to find a regular expression to find [number2] in [number],[number2][word].
So far I've tried with [,](\d*), but it also gets me the comma.
Demo: https://regexr.com/59eqa
You may use:
(?<=,)(\d*)
Regex Demo
Detail:
(?<=,): positive look behind that doesn't consume character but indicate that the number must have , before it
The previous answers do not handle the case that the second (or two numbers) is matched.
If the second number must be captured, this can be done with
\b\d+,(\d+)[A-Za-z]
where the "number2" is contained in captured group 1.
If you want to get the match only, you could use 2 lookarounds, asserting a comma to the left and a char a-zA-Z to the right.
Use \d+ to match 1 or more digits.
(?<=,)\d+(?=[a-zA-Z])
Regex demo
If there should be a digit before the comma as well:
(?<=\d,)\d+(?=[a-zA-Z])
Regex demo

Regex to find if all the characters in a word are the same specific character

I have a set of words coming in one by one like aa, ##, ???, ~~~, ?~ etc
I need a regex to find if any of these words is containing only ? or only ~.
Of the above input examples, ??? and ~~~ should match but not the others.
I tried ^[\s?]*$ and ^[\s~]*$ separately and it works, I am trying to combine them.
^[\s?||~]*$ doesn't work as it also recognizes ?~ as valid.
Any help?
You can use this regex, which looks for a string starting with a ~ or a ?, and then asserts that every other character in the string is the same as the first one using a backreference (\1):
^([~?])\1+$
Demo on regex101
You need to use backreference to achived your desired result.
If you want only ~ or ? use
^([~?])\1+$
If you want any repetitive pattern, use
^(.)\1+$
Explanation (.) or ([~?]) capturing the first charactor.
Then, \1+ checking the first charactor, one or more times (backreferencing)
You want to match lines that both start and end with any number of either a tilde or questionmark. That would be ^\(~\|?\)*$. The parentheses to make a group and the vertical bar to do the 'or' need to be backslash escaped.

What is the purpose of using positive lookarounds over not?

Say the string is ‘abc’ and the expression is (?=a)abc, would that not be the same as just searching for abc? When do positive lookarounds have purpose over not using them?
Positive lookahead works just the same. q(?=u) matches a q that is followed by a u, without making the u part of the match. The positive lookahead construct is a pair of parentheses, with the opening parenthesis followed by a question mark and an equals sign.
http://www.regular-expressions.info/lookaround.html
Here is a small example from https://ourcraft.wordpress.com/2009/03/25/positive-examples-of-positive-and-negative-lookahead/
Say I want to retrieve from a text document all the words that are immediately followed by a comma. We’ll use this example string:
What then, said I, shall I do? You shan't, he replied, do anything.
As a first attempt, I could use this regular expression to get one or more word parts followed by a comma:
[A-Za-z']+,
This yields four results over the string:
then,
I,
shan't,
replied,
Notice that this gets me the comma too, though, which I would then have to remove. Wouldn’t it be better if we could express that we want to match a word that is followed by a comma without also matching the comma?
We can do that by modifying our regex as follows:
[A-Za-z']+(?=,)
This matches groups of word characters that are followed by a comma, but because of the use of lookahead the comma is not part of the matched text (just as we want it not to be). The modified regex results in these matches:
then
I
shan't
replied

regular expression no characters

I have this regular expression
([A-Z], )*
which should match something like
test, (with a space after the comma)
How to I change the regex expression so that if there are any characters after the space then it doesn't match.
For example if I had:
test, test
I'm looking to do something similar to
([A-Z], ~[A-Z])*
Cheers
Use the following regular expression:
^[A-Za-z]*, $
Explanation:
^ matches the start of the string.
[A-Za-z]* matches 0 or more letters (case-insensitive) -- replace * with + to require 1 or more letters.
, matches a comma followed by a space.
$ matches the end of the string, so if there's anything after the comma and space then the match will fail.
As has been mentioned, you should specify which language you're using when you ask a Regex question, since there are many different varieties that have their own idiosyncrasies.
^([A-Z]+, )?$
The difference between mine and Donut is that he will match , and fail for the empty string, mine will match the empty string and fail for ,. (and that his is more case-insensitive than mine. With mine you'll have to add case-insensitivity to the options of your regex function, but it's like your example)
I am not sure which regex engine/language you are using, but there is often something like a negative character groups [^a-z] meaning "everything other than a character".