|
Does this simple Regex.Replace work for you?
string oldExp = @"__X(""(whatever1)"") alpha __X(""whatever2"") beta";
string newExp = Regex.Replace(Regex.Replace(oldExp, @"__X\(""", @""""), @"""\)", @"""");
modified 17-Sep-22 17:15pm.
|
|
|
|
|
By whatever1 and whatever2 I meant that these strings are arbitrary, not just exactly whatever1(2).
So it should work both for
__X(" (Joe)") John __X("Silver") Doe
with the result
" (Joe)" John "Silver" Doe
and
__X(" (Jane)") Michael J. __X("Frank Sinatra") Fox
with the result
" (Jane)" Michael J. "Frank Sinatra" Fox
I'd be happy even with replacing
__X(" (Joe)") John was there
for
" (Joe)" John was there
|
|
|
|
|
Regex that I gave you will work whatever the string inside parentheses is.
It focuses on removing the __X(" and ").
You can use this one for any __X
string newExp = Regex.Replace(Regex.Replace(oldExp, @"__\w+?\(""", @""""), @"""\)", @"""");
modified 18-Sep-22 13:41pm.
|
|
|
|
|
Is this possible to use in MS Visual Studio using 'Edit->Find and Replace->Replace in files' and checking 'Use regular expressions' and if so, then what should be in boxes 'Find what' and 'Replace with:' ?
In fact, it is a revert replace from
Find what: \("([^"]*)"(.*)
Replace with: (__X("$1")$2
modified 18-Sep-22 16:54pm.
|
|
|
|
|
Yes, it can be done from Find and Replace in two steps.
Find: __\w+?\("
Replace: "
Find: "\)
Replace: "
|
|
|
|
|
Hi moxol, thank you for your idea using two steps! I used this:
Find: __X\("([^"]*)"(.*)
Replace: "$1"q!@tr@$2
repeated n-times until no other replacement was made
Then simply remove all q!@tr@) strings without regexp to remove the original closing parenthese, where q!@tr@ is any improbable string used as an identifier
|
|
|
|
|
Lets say I want to "encrypt" the ID number 123456789 - add '*' to all the digits up to the last four - *****6789.
A starting point is to replace .*(....) with *$1. But it would produce *6789, which is too short.
How can I know how many chars the first .* matched?
|
|
|
|
|
It depends which language and framework you're using. But a regex is almost certainly the wrong tool for this job.
For example, in C# you might use something like this:
string input = "1234567890";
string output = string.Create(input.Length, input, static (buffer, input) =>
{
int pivot = buffer.Length - 4;
buffer[0..pivot].Fill('*');
input.AsSpan()[pivot..].CopyTo(buffer[pivot..]);
});
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
You could use a zero-length lookahead.
"1234345242342356789".replaceAll(/\d(?=\d{4})/g, "*")
|
|
|
|
|
Hi am looking for a RegEx that accepts a non-zero number only
accepted examples : -10, -12.50, 5, 200, 103.05 etc
not accepted : 0
Can you please help
|
|
|
|
|
It depends to a certain extent on what "non zero numbers" means, exactly. Does it mean 0 only and exactly, or does it have to not match 0. 0.0 0.00 etc.
For the simple case of a just excluding a zero we can use the following perl regex:
^[-+]?([1-9]\d+|[1-9]+.|\d*.\d+|0?.\d+)$
I've anchored this on line start and end ^ ... $ , but you might have other requirements. This will match e.g 1.2 -15 0.03 .12 15. and -0.3, and does not accept any of . , 0 , 0. , or 1.2.3 . However it does accept 0.0, 0.00, etc, and I have not yet found a way to eliminate them from the pattern. There might be a way using lookahead/lookbehind matches Lookahead and Lookbehind Tutorial—Tips &Tricks, but so far I haven't been able to figure out how to tell the regex engine that the lookahead/behind should match the entire pattern. For example we can eliminate 0.0 by adding a negative lookbehind regex of (?<!0.0) , but this also elminates 0.0 3, which is not what is wanted. Maybe someone else knows how to resolve this.
But maybe regex isn't the way to go. If you're working in Java or C# or C++ or ... , it's probably better to just read some input, check that the entire input parses as a number, convert to a number and then omit or complain if the value is zero - depending on what your use case is.
Keep Calm and Carry On
|
|
|
|
|
unfortunately this code is accepting 00, -0, 0.0,
I agree. I will look into handling it in JS.
Thanks for trying. Appreciate it.
|
|
|
|
|
If I have string
1213 494 4 toobad boys 1234 4444
I want to match "bad boys" and everything before and after bad boys until word boundary \b.
Something like this
"\\b.+?bad boys.+?\\b"
This returns
1213 494 4 toobad boys
and I want it to match "toobad boys"
I know that the problem is "\\b.+?bad" which matches everything up to "bad boys", but instead it has to match everything before "bad boys" until word boundary \b.
Help?
|
|
|
|
|
The problem is that your first \b is matching too early.
Try something like [A-Za-z]*bad boys[A-Za-z]*
In words, that will match "none or some alphabetic characters (no spaces!)" then "bad boys" then "none or some alphabetic characters"
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
I can't place [A-Za-z] since there will be also a non-Latin characters.
It has to be .+
|
|
|
|
|
|
Yes, I think
\w*bad boys\w* will do it.
It's MySQL Regex expression, and it seems that it works for both Latin and non-Latin characters.
|
|
|
|
|
What is Perl regex anchor to merely match at the position where the previous match ended, as anchor \G matches at the position where the previous match ended, and also at the first match attempt the \G matches at the start of the string in the way \A does.
|
|
|
|
|
Grateful to God who guide me to the best path
Perl's way
(?!^)\G
or
(?!\A)\G
|
|
|
|
|
|
Hello,
I am learning Regex.
I use Regex for a file-searching programme in Linux. (FSearch)
I do not understand, why we need ?= in Regex.
It looks for me like ?= is needless.
Why do I think so?
Here is an example:
abc(?=.*xyz)
But isn't this exactly the same like:
abc.*xyz
So actually my question is:
Can you give me an example, where ?= is really needed, because I can not get the same result without ?= ?
Would appreciate some answers.
Thank you.
modified 17-Jul-22 7:00am.
|
|
|
|
|
|
Thank you for the link.
In the meantime I think, I have found out, what the difference is between
abc(?=.*xyz) and abc.*xyz
We have 4 elements:
1. The regex search command string
2. The strings (e.g. the names of the files).
3. The strings that had a result (e.g. the file names that have a result).
4. The result for each string (The match within each string)
2-3 are the same for both abc(?=.*xyz) and abc.*xyz .
But 4. is different.
|
|
|
|
|
If you want to experiment with your expression, Google for "Expresso 3". It's free and an indispensable tool for understanding regular expressions.
|
|
|
|
|
Hello I was trying to understand how to work with a regex expression for when a User enters starts typing in 600 in the input field, then populate the error message. What would be the regex code for that?
Currently I have `^[\w'\-,.][^0-9_!¡?÷?¿\/\\+=@#$%ˆ&*(){}|~<>;:[\]]{2,}$`
|
|
|
|