Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I'm new to this forum and not very familiar with regex syntax.
I hope to get some help and tips here :-)
I am trying to find matches in strings that look like this:
Quote:

Any kind of text blablabla
Label: this url (abc.def.com)
any text
salkdj
Label: this url (asgd.xyz.org)
any text askjfh
found:check@xyz.org

What I need to match is
Quote:

Label: this url (asgd.xyz.org)
any text askjfh
found:check@xyz.

But what I get is nothing!

What I have tried:

I'm using regexr.com for testing.
I tried
/Label:.+?\((.+?)\..+?\)$.+?found.+?@\1/gms
but this ends up with no match.
When I remove the reference to the group "\1" at the end of the search string then I get this match
Quote:

Label: this url (abc.def.com)
lkasjdlskjdnh ushdua
salkdj
Label: this url (asgd.xyz.org)
askjfh
found:check@

But this is also not, what I need.
It seems to me as if the ".+?" preceding the "found" is not lazy.
And I don't understand, why the group reference evaluates to no match. What would be the right syntax to achieve my expected result?
Any help appreciated.
kind regards
Georg
Posted

This seems to work given your input and expected output:
RegEx
Label:[^(]+\([^.]+\.([^)]+)\).*found:[^@]+@\1
Demo[^]

  1. Literal text "Label:";
  2. Any character other than an open bracket, one or more times;
  3. Literal text "(";
  4. Any character other than a ".", one or more times;
  5. Literal text ".";
  6. Any character other than a closing bracket, one or more times, captured as group 1;
  7. Literal text ")";
  8. Anything at all, zero or more times;
  9. Literal text "found:";
  10. Any character other than "@", one or more times;
  11. Literal text "@";
  12. The same text captured in group 1;


However, your precise requirements aren't entirely clear. For example, does the "found" email always have the domain from the "label" line with just the first segment removed?
 
Share this answer
 
Comments
Maciej Los 8-Nov-23 8:00am    
5ed!
Patrice T 10-Nov-23 4:10am    
In case you haven't seen, OP use solutions to talk to you.
A problem is that you want to match only second label to the end, and sinxe "any text" includes "label", you need to tell.
Try
Label:.+(Label:.+found.+)

Quote:
I'm not very familiar with regex syntax.

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 
Hi Richard, I apologise if I have done anything wrong.
Firstly: I'm sorry if I left an unsatisfactory comment, that was not at all intentional. The solution to my problem was the "trick" of using the combination "[^(]" and "\(". That solved my problem with the \1 group. So I am satisfied.
(As I said, I'm not very familiar with regex)
Secondly: I wanted to apologise with my comment for coming up with a silly, simple example. If that was misleading, I apologise.
Kind regards
Georg
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900