|
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
|
|
|
|
|
If all you are doing is trying to validate (in a QLineEdit) that a floating point number is in a particular range, why don't you use a QDoubleValidator with it? This allows you to set range values[^].
|
|
|
|
|
I have two scenarios in table one is name of person and another is cityname but both name of the person and cityname starts with capital letter and followed by small letters
I want different regular expression for cityname and name of person but both starts with capital letter and followed by small letters how can I differentiate them with regular expression.
|
|
|
|
|
This makes no sense at all. What you've said is you have Person names and City names, both using the sexact same format, a capital letter followed by lower-case letters. There is no way to "differentiate them", whatever that means, with a single RegEx.
You're going to have to do a better job of explaining what the data you're dealing with is like, and a much better explanation of what you mean by "differentiate them."
|
|
|
|
|
I am working with Data masking in test data management there we are using one tool called javelin workflow to extract data from XML which is inserted into database and
To read data we are using regular expression pattern for name and cityname.
But our requirement is city name like Birmingham should be masked as fixed value as Norwich and name of person should be masked as random letters, but we are using same regular expression pattern for both and data is not masking as we expected so we want to differentiate them with regular expression
|
|
|
|
|
If this is coming from an XML file, you SHOULD have fields in the XML specific to each type of name. If not, you have very badly malformed data in the file making it pretty much useless, unless there is another field in the same record telling you what type of name is in the record. Without that discriminator field, the data you're pulling from the XML is useless.
You cannot use a RegEx to distinguish between a person name and a city name in the same field. It's just not possible, even for a human to determine by hand.
|
|
|
|
|
XML element looks like for name of person
<ab ov="Jeff" v="Jeff" id="1">
And in the same XML for cityname
<ab ov="Birmingham" v="Birmingham" id="2">
And in same XML cityname having all caps letter like
<ab ov="BIRMINGHAM" v="BIRMINGHAM" id="3">
So suggest me regular expression for Cityname for both cases Birmingham and BIRMINGHAM because I want to proceed name for different function and cityname for different function in data masking tool.
And regular expression for name of person like "Jeff"
modified 8-Feb-24 11:29am.
|
|
|
|
|
You really are determined to ignore what you're being told, aren't you?!
It is literally impossible to use a regex to determine whether a sequence of characters refers to a person, a city, a talking kangaroo, or a Vulcan mating ritual.
Your only hope is to get a massive database containing the name of every single city on Earth, and hope that anything that's not in that list refers to a person. And even that's not foolproof - for example, "Paris" could be a city or a person. Without more details, you have no way of knowing.
The data you're trying to process is garbage. If you need to be able to tell whether the data refers to a person or a city, then you need to go back to the people providing the data and get them to add something into the data to distinguish the two. Assuming they know the difference in the first place!
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|