|
Hello,
I need to write a regular expression search which will locate when a line ends with the same text as the preceding line, but does not have the same first 10 characters. So in this example:
[11:12:21] Hello this is Tom. How are you?
[11:14:08] Hello this is Tom. How are you?
. . . I would need to search for consecutive lines for which the text was the same after the time entered in brackets.
I know that this search:
FIND: ^.{11}(.*)$
REPLACE; $1
. . . will locate the first 11 characters and remove them.
This search:
FIND: ^((.{10}).*)(?:\r?\n\2.*)+
REPLACE: $1
. . . will locate lines where the first 10 characters are the same and remove them.
But I can't figure out how to structure the search so it checks the text from position 11 to the end of the line, and then checks if the text on the next line from the 11th character to the end of the line is the same.
|
|
|
|
|
You cannot do what you are asking with a regular expression.
(There is in fact a very wrong way to attempt this which is ridiculous and would lead to nothing but a maintenance nightmare.)
However in a programming language that uses regexes the algorithm that you would create would look like the following
1. Read a line
2. Parse the line to remove the timestamp.
3. Does it match the previous one? (Do whatever you want)
4. Otherwise save it for the next time
5. Go back to step 1 until there are no more lines to read.
|
|
|
|
|
-Hardsoul -housejazz
+Jean Michel Rotin
-Anita Perras
-St.Niccolo
+Mahalia -fr
to
Hardsoul -housejazz
Jean Michel Rotin
Anita Perras
St.Niccolo
Mahalia -fr
|
|
|
|
|
The regex to match a single character at the start of the line using regex for the major languages.
^[-+]
To match more than one.
^[-+]+
Replacing it is a different problem and it specifically depends on the programming language you are using and which you did not specify.
|
|
|
|
|
My campaigns all follow the same naming convention, i am trying to use a Regex_Extract formula in DataStudio to create a new custom dimension that only displays the 4th last element in the below campaign: "ctatext"
Each element is separated by a _
channel_product_country_medium_brand_offer_campaignname_ctatext_date_objective_0
Closest i have gotten is:
REGEXP_EXTRACT(Session campaign, '(.+){4}(?:[^_]+.)')
but that only returns the 4th character in the string .. can anyone help ?
|
|
|
|
|
How about something like:
_([^_]+)(_[^_]*){3}$ regex101: build, test, and debug regex[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Oh yes that has helped, thank you very much!
|
|
|
|
|
Hi,
I want to split the text above in three results.
Every result starts with the word "Leistung" and can optionally have a second line starting with "Zusatz".
My regex-expression ist currently this:
/Leistung [ ]*[\w\d äöüÄÖÜ]*\n[\w\d äöüÄÖÜ\*]*/gm
But this does not fit exactly.
This ist the text:
Leistung Armeotraining ET Anzahl 1
Zusatz *TextZusatz_Anf1*
Leistung Atemtherapie 30 Anzahl 2
Leistung Aktivierungsgruppe Anzahl 3
Zusatz *TextZusatz_Anf3*
The result should be:
Leistung Armeotraining ET Anzahl 1
Zusatz *TextZusatz_Anf1*
Leistung Atemtherapie 30 Anzahl 2
Leistung Aktivierungsgruppe Anzahl 3
Zusatz *TextZusatz_Anf3*
Can anyone help me with the regex-expression?
Thank you in advance!
Tobias
|
|
|
|
|
It will depend on the "flavour" of regex you're using, but something like this seems to work for me:
^Leistung.*$(\n^Zusatz.*$)? Demo[^]
Make sure your regex has the "global" and "multi-line" options set, so that $ matches the end of a line rather than the end of the input string.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you, that worked for me!
|
|
|
|
|
I am trying to create a question on google forms with the following conditions but having a hard time figuring out how to create a regular expression for this
What is your member ID?
- Must be an 8 digit number
- cannot be repeating numbers like 00000000 or 11111111 etc
- cannot be 12345678
Is there a way to come up with a regular expression to achieve this?
|
|
|
|
|
I think you want to do some "string exclusions" (e.g. "12345678", "00000000", ... "99999999"); ending with an inclusion (0-9).
https://stackoverflow.com/questions/2078915/a-regular-expression-to-exclude-a-word-string
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
I have a similar string with a C++ function __X
__X(" (whatever1)") alpha __X("whatever2") beta
and I should remove the __X function and the corresponding parentheses, so the result should be
"(whatever1)" alpha "whatever2" beta
The argument to the __X function is a string embedded in "" and sometimes additionally embedded in ()
I'd appreciate any help, thanks in advance 
|
|
|
|
|
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.
|
|
|
|