5 Replies - 351 Views - Last Post: 09 July 2020 - 02:07 PM Rate Topic: -----

#1 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 333
  • Joined: 13-March 12

Need some help with a Regular Expression

Posted 08 July 2020 - 09:31 AM

Here is my regular expression that almost works

(?|[a-zA-Z]+\s){1,3}|(?|[a-zA-Z]+\(\d{1,4}\))|(?|[a-zA-Z]+) |(?|[a-zA-Z]+\(\d{1,4}-\d{1,4}\))|((?|[a-zA-Z]+\s[a-zA-Z0-9]+\s|[0-9]+[a-zA-Z])[a-zA-Z0-9]*)



Here are the values being matched
apple pie (.25/lb)
peaches (6.00/bushel)
onion(1015) (2.50/ea)
something TO2K (3.85)

From that I get matches
apple pie
peaches
onion(1015)
something
and 2K

I want the last match to be "something TO2K." The number in there is messing with me. The thing is, what if the digit is in the second or third position? Also, is there a better way to have written what I have?

EDIT: All of this is fine except when I try it in VB. Then it doesn't like the (? at the beginning of each group!

This post has been edited by ScottinTexas: 08 July 2020 - 09:40 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Need some help with a Regular Expression

#2 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 333
  • Joined: 13-March 12

Re: Need some help with a Regular Expression

Posted 08 July 2020 - 11:44 AM

([A-Za-z]*\s[A-Za-z]+\(\d{1,3}-\d{1,4}\))|[A-Za-z]*\.\s[A-Za-z]*\(\d{3,4}\)|([A-Za-z]*\(\d{2,4}\))|([A-Za-z]+[0-9]*[A-Za-z]*\([0-9]*-[0-9]*\))|(([A-Za-z]+\s)*[A-Za-z]*[0-9]*[A-Za-z]+)


That's what I have worked out so far. It is returning the desired results with extras. It returns the text values inside the parenthesis, as in "bushel" from (6.00/bushel). The above expression may be in a different order that the first one posted because I scratched it and started with the first group.
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,567
  • Joined: 12-December 12

Re: Need some help with a Regular Expression

Posted 09 July 2020 - 01:02 AM

Do you really need complicated regex for this? All you really need to know is the last index of ( , you could additionally check that this is followed by a closing parens. Then perhaps look for the forward slash between if needed.
Was This Post Helpful? 1
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: Need some help with a Regular Expression

Posted 09 July 2020 - 05:18 AM

The last line with "(3.85)" does not have a forward slash between the last parenthesis.

ScottinTexas: Right now it doesn't look like you are trying to find matches for the entire string, but rather you want to match part of the string and remove it. Can you show us your whole code where you pass in the string into the regular expression and get back the set of matches? Are you wanting the regex to have the first group within the matches to correspond to your desired strings, and you are just discarding the other groups? What is the desired output for these inputs:
(310) 867-5309 (x337)
William Thurston (Senior) (age 65)
We have no parrots today.


Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,567
  • Joined: 12-December 12

Re: Need some help with a Regular Expression

Posted 09 July 2020 - 06:11 AM

View PostSkydiver, on 09 July 2020 - 12:18 PM, said:

The last line with "(3.85)" does not have a forward slash between the last parenthesis.

I noticed that ;). I suggested searching for the slash 'if needed', if it is necessary to distinguish (qty/units).
Was This Post Helpful? 0
  • +
  • -

#6 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 241
  • View blog
  • Posts: 1,303
  • Joined: 05-December 13

Re: Need some help with a Regular Expression

Posted 09 July 2020 - 02:07 PM

Why use regex for something so simple?

            const string str = "something TO2K(3.85)";
            string substr = str.Substring(0, str.IndexOf('('));
            Debug.Write($"Substring is : {substr}");
            /* Want the values after, in brackets? */
            string trailing_Values = str.Substring(substr.Length);
            Debug.Write($"Trailing values of substr are : {trailing_Values}");


Output :

Substring is : something TO2K

Trailing values of substr are : (3.85)

Is this what you are trying to do? And is it not easier to use substring and indexof?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1