This question already has answers here:
Difference between * and + regex
(7 answers)
Closed 4 years ago.
I know the following regex will match a single integer, as well as a list of comma delimited integers:
/^\d+(?:,\d+)*$/
How can i turn this into only matching a list of integers? A single integer should not match. 123,456 and 634,34643,3424 should match.
You would use the + operator meaning "one or more" times instead of * to repeat your group.
/^\d+(?:,\d+)+$/
Live Demo
Related
This question already has answers here:
regex: find one-digit number
(5 answers)
regular expression to search only one-digit number
(4 answers)
Closed 1 year ago.
I have large strings that contain dates, single digit numbers and double digit numbers. I need to find and pad the single digit numbers (which could also be in the dates, as the months and days can possibly be single digits) with a preceding zero. The data is in this format:
m/d/yyyy 1,21,3,42,5,63,7,84,9
I need it in this format
mm/dd/yyyy 01,21,03,42,05,63,07,84,09
I've tried this:
$pattern = "#[9]{1}#m";
$str = preg_replace($pattern, "09, $str);
It kind of works, but for numbers like 29 that shouldn't be touched, it turns it into 209. Ideally I'd like to use a wildcard instead of a specific number so that it'll just pad all of the single digit numbers in the string but I haven't quite figured that part out. Any and all help would be appreciated , thanks.
You can match a digit 1-9 not surrounded by digits
(?<!\d)[1-9](?!\d)
Regex demo
Replace with a zero and the full match 0$0
If you don't want to match the digits in the date, you can assert a comma or the end of the string to the right:
\b[1-9](?=,|$)
Regex demo
This question already has answers here:
Getting a number when it has certain prefix from a block of text in PHP
(3 answers)
Zero-pad digits in string
(5 answers)
Closed 1 year ago.
I am trying to add the int with strings in PHP but I could get a helpful answer on StackOverflow and other website communities.
I want the code separate the ALPHABETS and NUMBER and then add the value in NUMBER and again JOIN the ALPHABETS AND ADDED NUMBER together.
How I achieve this, Please help.
Example:
$mynumber = 'SBI0001';
$addValue = '1';
It will be 'SBI0002' in PHP.
This question already has answers here:
Can hyphens be used in query string values?
(2 answers)
Closed 1 year ago.
Hello to everyone who might read this question.
The question is very basic, can a query string parameter key contain the hiphen (-) char?
I have this url
https://www.example.com/page?uid=83485743jfj4f37gj348&thank-you
Thank you all.
Judging from
https://url.spec.whatwg.org/#application-x-www-form-urlencoded-percent-encode-set
you can use the hypen character without any escaping.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
I have this regex pattern:
[^-]+-(.+)[^\d](.+)-(.*?)-.*(\d+).*-([\w]+-[\w]+-[^-]+)-(\d+-\d+)-(.+)\.
That needs to match both these cases
Data Location 1 - many many words 201808206566 - many words - 010114-INL-USD-B087834-2018-08-Bill.PDF
Data Location 1 - many many words 201808206565 - many words - 010115-INL-B087845-2018-08-Bill.PDF
As is, this matches the first case and not the second. And I get the opposite result by removing one instance of [\w]+- from within the 5th capture group, this is because the first case contains INL-USD-B087834, which has an additional data block in it. How can I make the second instance optional?
Put it in an optional group using the ? operator.
[^-]+-(.+)[^\d](.+)-(.*?)-.*(\d+).*-(\w+-(?:\w+-)?[^-]+)-(\d+-\d+)-(.+)\.
Or you use a numeric quantifier to allow 1 or 2 word blocks there:
[^-]+-(.+)[^\d](.+)-(.*?)-.*(\d+).*-((?:\w+-){1,2}[^-]+)-(\d+-\d+)-(.+)\.
This question already has answers here:
Get string between two strings
(6 answers)
Closed 6 years ago.
I'm trying to get the string between two specific matches, for example two hashtags.
What I would like to achieve:
input: some text in front ##hi there## some text behind
output: hi there
With /%%(.*)%%/, ##hi there## is returned.
Thanks in advance
You need a look ahead and look behind.
(?<=##).*(?=##)
This will match hi there
https://regex101.com/r/qUR4NR/1