|
Hi,
I'm trying to get some expression using regexp that extracts highlighted numbers:
Traveller-CxFlight-895-20240819-1903
Traveller-ETDChange-45-20240819-1903
M-TX6102-20240819-FOC
M-TX7156/20240819
Any help would be appreciated.
|
|
|
|
|
Based on what you have here you're looking for the first string of digits in a string. Something like
^[^0-9]+([0-9]+).* should extract the digits. Vary for your flavour of regex
Explanation:
^ anchors to left
[^0-9]+ matches all non-digits (at least one. Maybe use *
if you may have digits at start of text
([0-9]+) extract a string of digits, at least one
.* matches rest of string
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
Your solution works great! Thanks a lot for your help
|
|
|
|
|
Thanks to all who helped to resolve this ,
I do appreciate you taking your time to help
with this problem.
HELP IS MUCH APPREXIATED
modified 17-Jun-24 21:34pm.
|
|
|
|
|
Back again with yet another nom de guerre eh?
|
|
|
|
|
What gave it away, the capitalization, QT, the filter on the response content?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
The excuse and instructions at the beginning of the generally messy question. Still can't (or won't) use <pre> tags properly. For some reason closed his previous account recently. So now comes back with a new id but the form of his question is the same as always.
|
|
|
|
|
Hello.
I'm trying to understand why '(?<=a)[1-9]+' works with "a556" but 'F(?<=a)[1-9]+' doesn't work with "Fa556".
Thanks!
|
|
|
|
|
I would suggest reading up on lookbehinds @ Advanced Regex Tutorial—Regex Syntax[^] . You see the expression (?<=) can be used both as lookbehind before the match and lookbehind after the match.
The second regex, due to the "F" being first in the expression means it finds a "F", then attempts to look for an "a" before the "F" which does not fit your example text. So this is a lookbehind after the match, your first regex was a lookbehind before the match.
You should put the "F" together with the "a" inside the lookbehind. Maybe you didn't for some reason known only to you. Since you have provided very little context for the 2 expressions and examples I can't help any further currently.
Terry
|
|
|
|
|
Hello and thank you very much for your reply.
Firstly i have to read the article you have mentioned and then we can talk again.
But i believe this
You see the expression (?<=) can be used both as lookbehind before the match and lookbehind after the match.
is the most important thing that i didn't know.
Can you choose if (?<=) will be treated as before or after only ??? (i will found out after reading the article
|
|
|
|
|
Hi
Been playing around but cant figure this out, I 'm looking for a way to identify where there is a space followed by the number 1 and a letter of the alphabet, below is an example of the text and I have underlined where I want the identification to be (so I can then replace/split at that point only) but can't seem to figure it out
-ABC046Y 1SMITH/FREDMR
.L/JCD5CQ
.R/TKNE HK1 6002300348396/1-SMITH/FREDMR
.R/SEAT HK1 9F
.R/FQTV AB 60042576-1SMITH/FREDMR 1DOE/JANEMISS
.L/JC93QW
.R/TKNE HK1 6002300219029/2-1DOE/JANEEMISS
.R/SEAT HK1 8A
.R/CHKD HK1 011-1DOE/JANEMISS 1PRESLEY/ELVISMR-A2
Tried a few things with regex editor but it always finds other things as well which I don't want
Hope someone can help
Many thanks
|
|
|
|
|
Try either "\s1[A-Za-z]" if the "space" could be any whitespace character, or " 1[A-Za-z]" if it has to be a literal space character.
Demo[^]
If you want to get fancy, and match each 1 which is preceded by a space and followed by a letter, without capturing the space and the letter, then you'll need zero-width assertions. Eg: "(?<=\s)1(?=[A-Za-z])"
Demo[^]
If that still doesn't work, you'll need to specify which regex "flavour" you're using - PHP, JavaScript, .NET, etc. - and provide more precise details of what you have tried and what the problem is.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: you're using - PHP, JavaScript, .NET, etc
At least for those I don't think it matters. All of those (and Java) use for the most part a fully compatible version of regex which matches Perl.
|
|
|
|
|
I'm trying to create a perl regular expression that matches a URL that is not preceded by an equal sign and one single or double quote (optional) ignoring whitespace. The code below gives an error:
Warning: preg_replace(): Compilation failed: lookbehind assertion is not fixed length at offset 0
I know my URL regular expression isn't perfect, but I'm more focused on how to do the negative lookbehind or how to express this in some other way.
For example, in the code below, I would like for the expression to match http://www.url1.com/ and http://www.url3.com/, but not the other URLs. How can I do this? The code below gives a warning and does not populate the $matches variable.
The Code:
$html = "
http://www.url1.com/
= ' http://www.url2.com/
'http://www.url3.com/
<a href='http://www.url4.com/'>Testing1</a>
<img src='https://url5.com'>Testing2</a>";
$url_pregex = '((http(s)?://)[-a-zA-Z()0-9@:%_+.~#?&//=]+)';
$pregex = '(?<!\\s*=\\s*[\'"]?\\s*)'.$url_pregex;
$output = preg_match('`'.$pregex.'`i', $html, $matches);
preg_match('`'.$pregex.'`i', $html, $matches);
var_export($matches);
Perl Regex in PHP, using ` instead of /:
'`(?<!\\s*=\\s*[\'"]?\\s*)((http(s)?://)[-a-zA-Z()0-9@:%_+.~#?&//=]+)`i'
modified 27-Mar-24 16:37pm.
|
|
|
|
|
I am using this regex to verify a password contains at least one letter, one number, min 7 chars in PHP, how do I modify to dissallow any spaces?:
/(?=.*\d)(?=.*[a-zA-Z]).{7,}$/
Thanks
|
|
|
|
|
Why?
I can just about understand enforcing "complexity requirements" to prevent users from using stupidly simple passwords that would be guessed in seconds. Although checking whether the password has been exposed in any data breaches[^] might be a better option.
But why block the user from using an entire class of characters in their password? That would suggest that either a) the password isn't being stored securely[^], or b) the password storage code is trying to avoid some sort of injection vulnerability by restricting the input rather than fixing the vulnerability.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I realise I've come to this post a long time after the request, but thought I'd still provide an actual way forward, possibly even the solution if one is still required. Or maybe it will serve some useful purpose for another poster with a similar need.
If you understand the regex you have provided you will understand the first 2 sections are only a lookahead to identify the requirements are met, they don't actually consume any characters.
The .{7,}$ is the part which grabs the password and at the moment it will grab any character (denoted by the DOT character). So it's here where you could do the final verification. What needs to happen is that the DOT changes to the "allowed" characters which exclude the space (and/or any other characters).
I don't do PHP regex but I would think replacing the DOT with something like \S or [^ ] or even [[:^space:]] would be appropriate. By removing a character from the allowed group, if the removed character is encountered the regex will fail. So no password is selected.
It could even be done with another lookahead and I think that could be (?=\S{7,}). If using the lookahead, then no need to change the DOT character for my other options.
Some background info:
\S is any character except whitespace which includes [ \t\r\n\v\f]. This is a capital S, which is the negated \s, so opposite of what \s means in PHP regex (whitespace).
[[:^space:]] is a negated PHP class, the ^ denotes NOT what follows.
Terry
|
|
|
|
|
I have to check the user input in some input fields for a float value range with Regex.
Here are my two float value ranges for which I have not yet found a solution:
```
Case 1. (ui >= 0.0001) & (ui <= 10000.0000)
example ok: 0.0001, 10000.0000
example fail: 0.0000, 10000.0001
Case 2. (ui >= 0.0000) & (ui <= 65536.0000)
example ok: 0.0000, 65536.0000
example fail: -0.0001, 65536.0001
I have been working on a solution for this for a long time. Unfortunately without success so far. I would be very happy if someone could help me.
|
|
|
|
|
Don't use regular expressions for this. Parse the values as floating point numbers, and then compare them to your acceptable range.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Also, if you use the library functions for parsing floating point values, they will/may consider the culture. In many European cultures, a decimal comma is used, not a decimal point. The point is used a digit group separator.
Programming code literals (almost?) always follows the English tradition of decimal point, and comma as group separator. User input is different. You can't make all users switch to a different number syntax just because your program doesn't honor the local culture.
A minor non-regex comment to the OP: If you intend the check
Case 2. (ui >= 0.0000) & (ui <= 65536.0000) to verify that the number can be converted to a 16 bit uint, then it should be either (ui < 65536.0000) or (ui <= 65535.0000).
(But then, I think it curious to verify a floating point number against the value limits of an integer type. A numeric value is either a measurement or a count. Re-interpreting a measurement as a count is a strange thing to do.)
Religious freedom is the freedom to say that two plus two make five.
|
|
|
|
|
I have found a solution for case 1:
^((0[\,\.]000[1])|([1-9]\d{0,3}(?:[\,\.]\d{1,4}))|(?:10000(?:[\,\.]0{1,4})))?$
However, the expression is not as difficult as is said here.
At the special request of a single person, the regular expression now also accepts "." and ",".
I program in QT and QLineEdit can be configured very nicely and effectively with regex expressions. I would therefore be very reluctant to deviate from this.
I am programming an LCR measuring bridge front end from Analog Devices (ADMX2001). The tDelay value range between 0.0000 and 65536.0000 is expected by the frontend and is also checked. And the value range does not fit into a uint16. Should I now discuss the specified value range with Analog Devices ? They must know in which way the bridge operates.
modified 9-Feb-24 13:37pm.
|
|
|
|
|
Dagobert1 wrote: At the special request of a single person, the regular expression now also accepts "." and ",".
That isn't what they said.
There are many users in many places that represent decimal numbers using a different form.
So you either do not want to support them with your solution or you do.
|
|
|
|
|
Here is the solution for case 1:
(ui >= 0.0000) & (ui <= 10000.0000):
^((0([\,\.]\d{0,4})?)|([1-9]\d{0,3}(?:[\,\.]\d{1,4})?)|(?:10000(?:[\,\.]0{1,4})))?$
(ui >= 0.0001) & (ui <= 10000.0000):
^((0[\,\.]000[1])|(0[\,\.]\d{0,3}[1-9])|([1-9]\d{0,3}(?:[\,\.]\d{1,4})?)|(?:10000(?:[\,\.]0{1,4})))?$
Maybe the solution will help someone else.
|
|
|
|
|
Dagobert1 wrote: Maybe the solution will help someone else.
I have been using regular expressions extensively for decades and the way I would solve the problem was already suggested in a previous post.
Parse the number into a floating point value and then validate it that way.
Even when I have needed to provide a configurable validation I have designed it that way.
It is not only less complex it is also going to be faster.
|
|
|
|
|
Now try any remotely-complicated range - eg: ui ≥ -19.4242 && ui ≤ 1337.4242 - and see how "easy" that is with a regex.
Parsing the value as an appropriate type and then checking the range is far simpler and faster. And as has already been pointed out, it will handle culture-specific formatting much better.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|