Click here to Skip to main content
15,883,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I am trying to create a regex for this string:

My super data plan 10gb mobile 99,00 1 jul - 31 jul 2020 99,00
99,00 is a variable, which could be either 1,00 or 1000000000,00

I need to extract following elements:
[My super data plan 10gb mobile][99,00][1 jul][31 jul][2020][99,00]

For now I have this to start with:
([a-zA-Z0-9_ ]*(\d{1,9},[0-9]*)\s*([0-9]*\s*jul)\s*[-]\s*([0-9]*\s*jul)\s*[0-9]*\s*(\d{2,4},[0-9]*))

But this does not work, because it produces the following result:
[My super data plan 10gb mobile 9][9,00][1 jul][31 jul][2020][99,00]

What I have tried:

I need it to match everything, but untill
99,00
so from my point of view it should match string until another regex, so something like this:
match
[a-zA-Z0-9_ ]*
but until
[0-9]*(,[0-9]*)
, so 10gb (which is number) should match in a string because it is part of a name, but 99,00 should not because it is a price.
Can someone help?
Thx
Posted
Updated 21-Sep-20 20:33pm
v3

Quote:
Can someone help?

The principle of regex is to match variable strings, to explain what you want, you need to give examples inputs and matched strings to let us know what you want, a few input that will be rejected may help too.
Technically, this regex will match what you told us:
My super data plan 10gb mobile

But I fear it is not what you want. Your explanation is not an explanation for people that don't already know what you want.
Show few examples like:
Input: "MyInput"
Match: "My" or not match
Condition: Because of ...


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[^]

[Update]
Force a space
([a-zA-Z0-9_ ]*\s(\d{1,9},[0-9]*)\s*([0-9]*\s*jul)\s*[-]\s*([0-9]*\s*jul)\s*[0-9]*\s*(\d{2,4},[0-9]*))
               ^ force space here
 
Share this answer
 
v3
Comments
csrss 22-Sep-20 2:03am    
I have updated my question
csrss 22-Sep-20 2:36am    
First of, thank you. It works.
This is strange, I am testing regex on regex101 website, while this works when I open it on one computer I am now, does not when opened on another laptop - on which I was testing. Magic.
Maciej Los 22-Sep-20 2:38am    
Alternativelly you can match groups ;) Please, see my answer.
Maciej Los 22-Sep-20 2:36am    
5ed!
Patrice T 22-Sep-20 2:39am    
Thank you
[EDIT]
If you would like optionally to get first occurance of 99,00...
For lines:
My super data plan 10gb mobile 99,00 1 jul - 31 jul 2020 99,00
My super data plan 10gb mobile 1 jul - 31 jul 2020 99,00


Try this:
^(?<text>[\w\s]+)(?<num1>[\d]{2,},[\d]{2}){0,}\s(?<fromdate>\d{1,}\s\w{3})\s-\s(?<todate>\d{1,}\s\w{3})\s(?<year>\d{4})\s(?<num2>\d{2,},\d{2})$


Example - version 5[^]

Matches:
1.
Group `text`	0-31	My super data plan 10gb mobile 
Group `num1`	31-36	99,00
Group `fromdate`	37-42	1 jul
Group `todate`	45-51	31 jul
Group `year`	52-56	2020
Group `num2`	57-62	99,00

2.
Group `text`	63-93	My super data plan 10gb mobile
Group `fromdate`	94-99	1 jul
Group `todate`	102-108	31 jul
Group `year`	109-113	2020
Group `num2`	114-119	99,00


As you can see, in both cases group text is fetched correctly (to the first occurance of 99,99 - num1 or to the first occurance of 1 jul - fromdate). num1 is fetched only if exists! {0,} - is used to define the number of occurencies for this group.

Good luck!
 
Share this answer
 
v2
Comments
Patrice T 22-Sep-20 2:40am    
+5 too
Maciej Los 22-Sep-20 2:52am    
Thank you.
csrss 22-Sep-20 2:47am    
Yep, thanks. Still space is needed of course:
^(?P<text>\w.*)\s+(?P<num1>[\d]{2,},[\d]{2})\s(?P<fromdate>\d{1,}\s\w{3})\s-\s(?P<todate>\d{1,}\s\w{3})\s(?P<year>\d{4})\s(?P<num2>\d{2,},\d{2})$
Maciej Los 22-Sep-20 2:56am    
Nope. You don't need a space, because i'm using the wildcards to grab a space too ;)
BUT (!) it depends on your needs :)
csrss 22-Sep-20 3:15am    
Hmmm, I have modified your regex this way:
^(?P<text>\w.*)\s+(?:(?P<num1>[\d]{2,},[\d]{2}))?\s(?P<fromdate>\d{1,}\s\w{3})\s-\s(?P<todate>\d{1,}\s\w{3})\s(?P<year>\d{4})\s(?P<num2>\d{2,},\d{2})$
Because I want to make a <num1> optional. But so far I have no luck in making it work, because if I remove <num1>, then a line is required 2 spaces. If I make one space optional - this is not working. Any idea, how can <num1> can be made optional?

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