Click here to Skip to main content
16,015,900 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have the following 2 type of string which I need to use the same regex for.

string 1 :
2022.08.14 (19h45) - Salernitana 0-1 AS Roma (suppercoppa)

string 2 :
2022.08.14 (19h45) - Salernitana 0-1 AS Roma


I need to use one regex so I can get 3 different groups

Group1: Date
Group2: Team Name 1 (Salernitana)
Group3: Team Name 2 (AS Roma)

What I have tried:

I tried the following regex which is working for string 2 but not for string 1:
([0-9]+\.[0-9]+\.[0-9]+).*- (.*[a-zA-Z]) .*[0-9] ([a-zA-Z]+.*)
Posted
Updated 20-Aug-22 20:52pm
v3
Comments
Patrice T 20-Aug-22 13:22pm    
Should you have a match with
2022.08.14 (19h45) - AS Roma 0-1 Salernitana
Joe Doe234 20-Aug-22 13:34pm    
sorry?
Patrice T 20-Aug-22 14:22pm    
Should it work with "2022.08.14 (19h45) - AS Roma 0-1 Salernitana" ?
Joe Doe234 21-Aug-22 1:41am    
yes it works for that text but not for the other text, I want it to work for both of them

When I run your regex (in Expresso) against either string I get similar results:
String 1:
1: 2022.08.14
2: Salernitana
3: AS Roma (suppercoppa)

String 2:
2022.08.14
2: Salernitana
3: AS Roma

So ... what am I doing that you aren't? How did you test your string? Have you tried Expresso[^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
Comments
Joe Doe234 20-Aug-22 12:44pm    
the "(super coppa)" should not be in group 3, the group 3 need to read all characters until '(' or end of line '$', but I cannot do
it :(
OriginalGriff 20-Aug-22 13:41pm    
So add that to your regex!
In Expresso, look under the expression builder, Groups - you'll see a "match suffix but exclude it" group.
Joe Doe234 21-Aug-22 1:48am    
let me try
Try this RegEx with groups 1, 2 and 4
([0-9.]{10}).+- ([a-zA-Z]+( [a-zA-Z]+)*).+\d ([a-zA-Z]+( [a-zA-Z]+)*)

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
 
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