How can I match this string '-,-,-,9,-'? - regex

I was told to validate the string like this -,-,-,9,-
It was separated by , and contains 1 number(0-9), others are all -
some examples:
9,-,-,-,-
-,-,-,-,9
-,-,2,-,-
How can I match this? And what concepts should I learn in regex?
Update
I miss the times, sorry, this string can only contains 5 part,so the length can be only 9,it means a string like below should not be passed:
-,-,9,-,-,-
and of course, it should have only one number.

^(?=\D*\d\D*$)[0-9-](?:,[0-9-]){4}$
You can try this.See demo.
https://regex101.com/r/nM7nT5/5

This ensures that the string must hav atleast one comma and exactly one digit.
^(?:\d(?:,-)+|-(?:,-)*,\d(?:,-)*)$
DEMO
OR
^(?=\D*(?:^|,)\d(?:,|$)\D*$)[\d-](?:,[\d-])+$
DEMO

Related

Return dash followed by a single character

This works as expected:
([^\u0000-\u007F])+-हा([^\u0000-\u007F])+
Returns:
ब-हाणपूर
ब-हाणी
बनियन-हाफ
But I am looking for 1 character followed by dash. The expected output is:
ब-हाणपूर
ब-हाणी
I tried to replace + sign with character count like this...
([^\u0000-\u007F]){1}-हा([^\u0000-\u007F])+
But it returned the same 3 results. How do I return the first 2?
You need anchors:
^([^\u0000-\u007F])-हा([^\u0000-\u007F])+$
Demo
You asked 'What if I need 5 characters to the left of dash?'
The regex portion [^\u0000-\u007F] as written matches a single character that meets that criterion. If you want more or less than one, use a regex quantifier to describe how many you want.
In this case, if you want 5, you would use:
^([^\u0000-\u007F]{5})-हा([^\u0000-\u007F])+$
Probably like this:
^([^\u0000-\u007F]){1}-हा([^\u0000-\u007F])+
^([^\u0000-\u007F]{1})-हा([^\u0000-\u007F]+)
(\b[^\u0000-\u007F]{1})-हा([^\u0000-\u007F]+)
Regex demo

Get some piece of string regex

I have the strings below and I just want to get the value of string: AttributeReferenceID. What I need to do?
I tried this [A]ttributeReferenceID (?<referenceID>\d+) but can't success. The string that I want is at any part of the log, so the string could be at the first line, second or in the last line.
String to get:
AttributeReferenceID 123
AttributeReferenceID 456
AttributeReferenceID 789
String to discard:
ISCCAttributeReferenceID 091281 [09123na0]
ISCCAttributeReferenceID 123012 [i1208221]
ISCCAttributeReferenceID 091221 [0oas9019]
If you regex flavor accepts positive lookbehinds, you can use the following
(?<=^AttributeReferenceID\s{4})(\d+)
Demo
The regex will look before the match if there is the specific string you're searching for followed by 4 spaces. If the length of the spaces may vary, then you'll have to find another solution. The following should normally work.
(?:^AttributeReferenceID\s+)(\d+)
Demo
And take the first group.

Regex: match patterns

So I have a string of numbers. There are certain rules I need.
It can have tel1 or tel2 at start (or not)
If E is in number it must be foolowed by 4 digits then followed by 49. (Optional pattern)
So a string like:
tel1: +E1234498912345678,tel2: +498912345678,tel1: +E123449D1238912345678,tel2: +E1234498912345678
is valid
tel1: +E12344598912345678,tel2: +498912345678,tel1: +E123449D1238912345678,tel2: +E1234498912345678
is invalid (first element invalid)
And also each element must begin with + like in examples
UPDATE: Also needs to match numbers with '#' suffix
This will work:
^((\s*tel[12]:\s*)?\+(E\d{4}49|\d)[^,]*(,|$))+$
Try the demo here.
^((tel1|tel2)?(:\s*)?\+(E)?\d{4}49\w+(,|$)|(tel1|tel2)?(:\s*)?\+(?!E)\w+(,|$))+$
You can try this.See demo.
http://regex101.com/r/iO1uK1/3
You could try the below regex,
^\s*(?:(?:tel[12]):\s*\+(?:E(?=\d{4}49)\S+?\b|\d+))(?:,(?:(?:tel[12]):\s*\+(?:E(?=\d{4}49)\S+?\b|\d+)))+$
DEMO
Demo : http://regex101.com/r/oG6lH3/1
(tel\d: |)\+(E\d\d\d\d49|49)
This will also match tel3 or tel9. If that is an issue, use one of the other provided answers.

Comma Separated Numbers Regex

I am trying to validate a comma separated list for numbers 1-8.
i.e. 2,4,6,8,1 is valid input.
I tried [0-8,]* but it seems to accept 1234 as valid. It is not requiring a comma and it is letting me type in a number larger than 8. I am not sure why.
[0-8,]* will match zero or more consecutive instances of 0 through 8 or ,, anywhere in your string. You want something more like this:
^[1-8](,[1-8])*$
^ matches the start of the string, and $ matches the end, ensuring that you're examining the entire string. It will match a single digit, plus zero or more instances of a comma followed by a digit after it.
/^\d+(,\d+)*$/
for at least one digit, otherwise you will accept 1,,,,,4
[0-9]+(,[0-9]+)+
This works better for me for comma separated numbers in general, like: 1,234,933
You can try with this Regex:
^[1-8](,[1-8])+$
If you are using python and looking to find out all possible matching strings like
XX,XX,XXX or X,XX,XXX
or 12,000, 1,20,000 using regex
string = "I spent 1,20,000 on new project "
re.findall(r'(\b[1-8]*(,[0-9]*[0-9])+\b)', string, re.IGNORECASE)
Result will be ---> [('1,20,000', ',000')]
You need a number + comma combination that can repeat:
^[1-8](,[1-8])*$
If you don't want remembering parentheses add ?: to the parens, like so:
^[1-8](?:,[1-8])*$

Regex match any character 5 or more times

I have this regex pattern:
^[.]{5,}$
Which I want to return true if the tested string has 5 or more characters.
I.E it'll only return false if the string contains 4 or less characters.
At the moment it seems to return true regardless of the number of characters and I can't see why.
You want
^.{5,}$
But really - just use the built-in string length function of the language of your choice
Try this regex:
.{5,}
more chars to make up the minimum post...
I think you dont need the ^ and $. Try just:
.{5,}