Need some help with regular expression
-
I am trying to match the following sample patterns: - 1 - 23-4B - 2,1-12 - 15A - 2-5,12 - 12A-4 so I tried the following regex but it seems it only matches some of above patterns... ^\d+[A-Z]?(-|,)?\d+[A-Z]?(-|,)?\d+[A-Z]?$ For example, the above matches all samples but not - 1 - 15A - 2,5 What did I miss here...
-
I am trying to match the following sample patterns: - 1 - 23-4B - 2,1-12 - 15A - 2-5,12 - 12A-4 so I tried the following regex but it seems it only matches some of above patterns... ^\d+[A-Z]?(-|,)?\d+[A-Z]?(-|,)?\d+[A-Z]?$ For example, the above matches all samples but not - 1 - 15A - 2,5 What did I miss here...
Hi! :) Try this: ^\d*(-|,)?\w*(-|,)?\w*$ They all start with a indefinite digit at the beginning of string: ^\d* In the middle there is optional characters: (-|,)? Ending with either digit or letter: \w* Hope it helps. Nice day! :thumbsup:
-
Hi! :) Try this: ^\d*(-|,)?\w*(-|,)?\w*$ They all start with a indefinite digit at the beginning of string: ^\d* In the middle there is optional characters: (-|,)? Ending with either digit or letter: \w* Hope it helps. Nice day! :thumbsup:
Thank you for the reply. However, the above expression doesn't match the following test cases: -2C-5,3 -2,1-12 -2-5,12
-
I am trying to match the following sample patterns: - 1 - 23-4B - 2,1-12 - 15A - 2-5,12 - 12A-4 so I tried the following regex but it seems it only matches some of above patterns... ^\d+[A-Z]?(-|,)?\d+[A-Z]?(-|,)?\d+[A-Z]?$ For example, the above matches all samples but not - 1 - 15A - 2,5 What did I miss here...
-
Thank you for the reply. However, the above expression doesn't match the following test cases: -2C-5,3 -2,1-12 -2-5,12
Hi! You did not list them from the beginning. But, as you do say about them now, it is matching: ^\d*(\w+)?(-|,)?\w*(-|,)?\w*$ Have a nice day!
-
You have six different patterns to match so regex is probably not the optimum choice of tool.
Hello, sir! Your above comment intrigued me and made me curious. Would you care to share something or a link that would help me what would a optimum choice of tool for over six different patterns be. I want to learn to optimize my work. Thank you, sir!
-
Hello, sir! Your above comment intrigued me and made me curious. Would you care to share something or a link that would help me what would a optimum choice of tool for over six different patterns be. I want to learn to optimize my work. Thank you, sir!
-
Member 15245024 wrote:
share something or a link that would help me
Yes, but I have no idea what problem you are trying to solve.
Hello, sir! Yes. I have a case that I defined hopefully in a accurate way as being a case of various, simultaneous and multiple replacements. I do not even know if it is even possible. I have a text, a long one, and sprinkled all over the place in the text, in the beginning of the string, in the middle, at the end, in parenthesis or not, with such expressions of 8 kinds: Abc. 22 Defg 22 Hijkl 22 Mnoprs 22 2 Tuvx 22 2 Zcb 22 2 Fh 22 C.C. 22 I need to replace each of the above with these below: Abc 22 Def 22 Hij 22 Mno 22 2Tu 22 2Zc 22 2Fh 22 Cal 22 This is the kind of situation I have, but not exactly these letters and digits - these are a dummy text as an example. Is it possible to do this simultaneously in parallel and in the same time? What would you use? THANK YOU, sir.
-
Hello, sir! Yes. I have a case that I defined hopefully in a accurate way as being a case of various, simultaneous and multiple replacements. I do not even know if it is even possible. I have a text, a long one, and sprinkled all over the place in the text, in the beginning of the string, in the middle, at the end, in parenthesis or not, with such expressions of 8 kinds: Abc. 22 Defg 22 Hijkl 22 Mnoprs 22 2 Tuvx 22 2 Zcb 22 2 Fh 22 C.C. 22 I need to replace each of the above with these below: Abc 22 Def 22 Hij 22 Mno 22 2Tu 22 2Zc 22 2Fh 22 Cal 22 This is the kind of situation I have, but not exactly these letters and digits - these are a dummy text as an example. Is it possible to do this simultaneously in parallel and in the same time? What would you use? THANK YOU, sir.
Because of all the differences I think you probably need to use a translate table. Create two lists of strings, the first is the strings to search for, and the second is the matching replacement. You would then need to go through each table for each line of text looking for strings in table 1. For each match you need to replace it with the appropriate entry in table 2. I am not sure how you could do this in parallel processing as it is really a sequential process.
-
I am trying to match the following sample patterns: - 1 - 23-4B - 2,1-12 - 15A - 2-5,12 - 12A-4 so I tried the following regex but it seems it only matches some of above patterns... ^\d+[A-Z]?(-|,)?\d+[A-Z]?(-|,)?\d+[A-Z]?$ For example, the above matches all samples but not - 1 - 15A - 2,5 What did I miss here...