regex masking patterns [closed] - regex

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have these inputs:
DFDBDFDFDF21R123
DFDBDFDFDF34R123
I want to match these inputs, except positions 9 and 10, like below:
DFDBDFDFxxR123
DFDBDFDFxxR123
So, to be clear: match 1-8, exclude 9-10, match 11-16.

To expand upon the answer from https://stackoverflow.com/users/557597/sln of
(.{8})..(.+)
The 'thing' you are missing from your understanding of Regex is 'grouping'
(SOME MATCHING SUB-STRING A)(SOME MATCHING SUB-STRING B)
If you use regex like this, you can do lots of nice things including 'pull out' parts of a line and then re-arrange them. But it also helps you group 'parts' that you want to search for.
so his
.{8}
matches '.' which is 'any single character' and then {8} means 'match any single character 8 times.
(.{8})
means 'group the first 8 characters' for use.
..
means 'match any two characters'
.+
means 'match 1 or more of 'any character'
(.+)
means "group that 1 or more of 'any character' for later use"
Therefore...
When you put them all together you get
(.{8})..(.+)
Which means 'match the first 8 characters (any 8 characters) as group 1' then 'any two characters' then '1 or more characters as group 2'
This would allow you to (depending on your regex client/etc.) is use $1 and $2 to print out, use or ...whatever... the values of group 1 and/or group 2.
Hope this helps.

Related

Regex to format numbers in italic [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 26 days ago.
Improve this question
I would like to format many chains of a text in italic. They are of the type 'Arbre 10523' I can capture them with
(Arbre)\b [0-9]{3,5}
but I do not know how to transform all these words in italic, if I replace the chain is just erased.
Thanks
Are you talking about LibreOffice Writer? If so, you can use the Find & Replace function from the ribbon (or Ctrl+H). Make sure to select 'Match Case' for case-sensitive matching, 'Regular Expresions' and select 'Italic' under Format:
I used the pattern:
\b(Arbre \d{3,5}(?!\d))
\b - Word-boundary to assert position is not preceded with other word-characters;
( - Open a capture group for backreference in the replace field;
Arbre \d{3,5} - Match 'Arbre ' followed by 3-5 digits;
(?!\d) - Negative lookahead to assert position is not followed by more digits;
) - Close capture group.
Now refer to $1 in your replace field. Then click replace all:

How do I get my regular expression to work with 3 numbers or more when two numbers are the same [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
The below regular expression works with 012, 201, 102, etc. I am trying to change the regular expression so that it matches 002, 200, 020O from a 4 digit number. I tried varies methods, but the regular expression is matching other patterns. Can someone give me some direction on how to resolve this issue. Thank you.
Working:
RegEx012 = re.compile(r'\b(?=[1-9]*0)(?=[02-9]*1)(?=[013-9]*2)\d+\b')
Not Working:
RegEx002 = re.compile(r'\b(?=[1-9]*0)(?=[1-9]*0)(?=[013-9]*2)\d+\b')
Results:
0250(good)
0260(good)
2052(bad)
2062(bad)
If you want to match a string with 2 times a zero and at least 3 digits, you could use a positive lookahead:
\b(?=[1-9]*0[1-9]*0[1-9]*\b)\d{3,}\b
Explanation
\b Word boundary
(?= Positive lookahead, assert what is on the right contains
[1-9]*0[1-9]*0[1-9]*\b Match 2 times a zero between optional digits 1-9
) Close lookahead
\d{3,} Match 3 or more digits
\b Word boundary
Regex demo
Or the other way around, assert 3 digits and match 2 times a zero between optional digits 1-9
\b(?=\d{3})[1-9]*0[1-9]*0[1-9]*\b
To match when the third character is a 3 (Or use a character class [03] to match either a 0 or 3)
\b(?=[1-9]*0[1-9]*0[1-9]*\b)\d{2}3\d*\b
Regex demo

Using captured groups in Regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to match a string where there are 5 characters where first four characters are A-Z and 5th is a digit. Also the first and the fourth character should be same.
I have a regex: [A-Z]{4}\d
However this wont check if 1st and 4th character are same. Please help
Regex: ^([A-Z])[A-Z]{2}\1\d$
1. ^ start of string.
2. ([A-Z]) capture first character.
3. [A-Z]{2} match next two character which can be in A-Z
4. \1 using captured group which contains first character of string.
5. \d a digit which can be 0-9
6. $ for end of string.
Regex demo

Filter lines based on range of value, using regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
What regex will work to match only certain rows which have a value range (e.g. 20-25 days) in the text raw data (sample below):
[product-1][arbitrary-text][expiry-17days]
[product-2][arbitrary-text][expiry-22days]
[product-3][arbitrary-text][expiry-29days]
[product-4][arbitrary-text][expiry-25days]
[product-5][arbitrary-text][expiry-10days]
[product-6][arbitrary-text][expiry-12days]
[product-7][arbitrary-text][expiry-20days]
[product-8][arbitrary-text][expiry-26days]
'product' and 'expiry' text is static (doesn't change), while their corresponding values change.
'arbitrary-text' is also different for each line/product. So in the sample above, the regex should only match/return lines which have the expiry between 20-25 days.
Expected regex matches:
[product-2][arbitrary-text][expiry-22days]
[product-4][arbitrary-text][expiry-25days]
[product-7][arbitrary-text][expiry-20days]
Thanks.
Please check the following regex:
/(.*-2[0-5]days\]$)/gm
( # start capturing group
.* # matches any character (except newline)
- # matches hyphen character literally
2 # matches digit 2 literally
[0-5] # matches any digit between 0 to 5
days # matches the character days literally
\] # matches the character ] literally
$ # assert position at end of a line
) # end of the capturing group
Do note the use of -2[0-5]days to make sure that it doesn't match:
[product-7][arbitrary-text][expiry-222days] # won't match this
tested this one and it works as expected:
/[2-2]+[0-5]/g
[2-2] will match a number between 2 and 2 .. to restrict going pass the 20es range.
[0-5] second number needs to be between 0 and 5 "the second digit"
{2} limit to 2 digits.
Edit : to match the entire line char for char , this shoudl do it for you.
\[\w*\-\d*\]\s*\[\w*\-[2-2]+[0-5]\w*\]
Edit2: updated to account for Arbitrary text ...
\[(\w*-\d*)\]+\s*\[(\w*\-\w*)\]\s*\[(\w*\-[2-2]+[0-5]\w*)\]
edit3: Updated to match any character for the arbitrary-text.
\[(\w*-\d*)\]\s*\[(.*)\]\s*\[(\w*\-[2-2][0-5]\w*)\]
.*\D2[0-5]d.*
.* matches everything.
\D prevents numbers like 123 and 222 from being valid matches.
2[0-5] covers the range.
d so it doesn't match the product number.
I pasted your sample text into http://regexr.com
It's a useful tool for building regular expressions.
You can try this one :
/(.*-2[0-5]days\]$)/gm
try it HERE

validating last two characters to follow a pattern [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a string abcdefgh,
i want to check if the last two characters are alpha numeric/alphabets but not numeric
i.e. it can end in a1, 1a, aa but not 11
are there any regex gurus who can chipin
The regex should return the below results for the strings
abcd - True
abc1d - True
abcd1 - True
abc12 - false
This should do it:
^.*(\d[a-zA-Z]|[a-zA-Z](\d|[a-zA-Z]))$
Online regex tool demo here.
Meaning:
^ the beginning of the string
.* any character except \n (0 or more times)
(
\d[a-zA-Z] a digit (0-9) followed by any character of a to z, A to Z
| OR
[a-zA-Z] any character of a to z, A to Z followed by
(\d|[a-zA-Z]) a digit or any character of a to z, A to Z
)
$ end of the string
Notice this matches the whole string, not only the two last chars, having those at the matched group one.
So, a letter and a digit, or a digit and a letter, or two letters?
([a-zA-Z]\d|\d[a-zA-Z]|[a-zA-Z][a-zA-Z])$
Depending on the regex system you're using, you can also use character classes such as :alpha:, but this one will work for all of them. Some regex syntaxes need a backslash in front of the parentheses and/or pipe symbol.
You can use
^.*[A-Za-z].|.[A-Za-z]$
Online test
You can use :
^.*([a-zA-Z]{2}|(\d[a-zA-Z])|([a-zA-Z]\d))$
DEMO
EXPLANATION: