Link to home
Create AccountLog in
Avatar of CoreNeter
CoreNeter

asked on

regular expressions, match several patterns OR

Hey
I am trying to match: a number, either round number or scientific number or float number or match data between single quotes

data1 = 2,
data2 = 2E-15,
data3 = 22.00,
data4 = 'whatsinbetween',

My current expression works for round numbers and scientific numbers
[0-9]+| -?[1-9](?:\.\d+)?[Ee][-+]?\d+

Need help with:
- If there are single quotes, i need it to match whatever data is in between
- For float numbers right now it returns it as 22 and 00 seperately as opposed to (22.00).

Any help appreciated
Thanks
Avatar of ddrudik
ddrudik
Flag of United States of America image

What range do you have for your float numbers etc.?  Your pattern doesn't seem to follow your example.
Avatar of CoreNeter
CoreNeter

ASKER

data1 = 20,
 data2 = 1.000000000E-009,
 data3 = 20.00000000,
 data4 = 'somethinginbetween'

data4 could go up to several thousand i.e. 10000
data2 will always be positive, E could go from 1 to 3 digits (power)
let me rephrase

 data1 = 20,
 data2 = 1.000000000E-009,
 data3 = 20.00000000,
 data4 = 'somethinginbetween'

data3 could go up to several thousand i.e. 10000.0000
data2 will always be positive, E could go from 1 to 3 digits (power)
Here's a pattern to start with:
Dim re As Regex = New Regex("'[^']*'|\d+\.\d+E[-+]\d{1,3}|\d+\.\d+|\d+",RegexOptions.IgnoreCase)
([+-]?(?=\d|\.\d)\d*(?:\.\d*)?(?:[Ee][+-]?\d+)?|"[^"]*")
If you want to test an entire string:
Dim re As Regex = New Regex("^'[^']*'|\d+\.\d+E[-+]\d{1,3}|\d+\.\d+|\d+$",RegexOptions.IgnoreCase)
Thanks alot ozo, ddrudik.
([+-]?(?=\d|\.\d)\d*(?:\.\d*)?(?:[Ee][+-]?\d+)?|"[^"]*")
Ozo i tried yours and it works great but it doesn't match what's in between data4 (the single quotes'
Usually there will be text in there, how can i add an or statement so that it matches whatever is in between the opening and closing single quote.

Thanks!
so basically I need ([+-]?(?=\d|\.\d)\d*(?:\.\d*)?(?:[Ee][+-]?\d+)?|"[^"]*") but add an OR that matches whatever is in between single quotes 'somethinghere' if no number matches are found. The data in between single quotes will most likely have symbols and no numbers.

 data3 = 20.00000000,
 data4 = 'somethinginbetween'
 data5 = '\?\'
 



CoreNeter, did you try my pattern?  It works with all of your examples, including your 'text' requirement.
hey ddrudik

this is what i'm trying in C# and it seems like its only matching the float and scientific notation number

new Regex("^'[^']*'|\\d+\\.\\d+E[-+]\\d{1,3}|\\d+\\.\\d+|\\d+$",RegexOptions.IgnoreCase);
SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
CoreNeter, please show the rest of your code, I don't see where the doubling of \ should be required with C#.NET.
Regex rx = new Regex("^'[^']*'|\\d+\\.\\d+E[-+]\\d{1,3}|\\d+\\.\\d+|\\d+$",RegexOptions.IgnoreCase);
Match match = rx.Match(line[i]);
                       
if i dont use double quotes I get "Unrecognized escape sequence" for each \ not doubled
The quote doubling worked with ozo's pattern

I just read it online this was the way to do it, otherwise using Regex.Escape("") which I didn't get working.
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Hi CoreNeter;

Have you tried my post yet?

String pattern = @"\b(\d+\.\d+E-\d{1,3}\d+|\d+\.\d+|\d+)\b|('(\w+)')";

Fernando
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
PERFECT!
thanks everyone
thanks everyone
Thanks for the question and the points.