0

I have a string where I need to parse the values such that it some times exist between[''] and sometimes not.

Eg:

Input
    1. blah .... REASON: ['elm H1MM_rr'], blah ....
    2. blah .... REASON: elm H1MM_rr, blah .... 
    3. blah .... REASON: elm H1MM_rr. 
    4. blah .... REASON: ['elm H1MM_rr']. 

The elm H1MM_rr is just an example here. It could be any string after REASON:

I tried REASON: ([^,. ]+)")but this still not working for ['']

Output I am looking for = elm H1MM_rr

Can some one suggest me a regex(java pattern) which can work for both strings ?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
user3407267
  • 1,524
  • 9
  • 30
  • 57

1 Answers1

1

(\[')?elm('])? should do the trick. This will match elm, ['elm, elm'], and ['elm'].

You also may want to use (?:\[')?elm(?:'])?. (Check the documentation that I've linked below.)

? will match something 0 or 1 times

You have to escape the first [, otherwise it treats everything between the [ and ] as a character set.

According to Your Edits

Now that you have clarified certain things and modified your question, the following code should work (at least in JavaScript):
REASON: (\[')?[\w/]{0,}(\s)?[\w/]{0,}('\])?
REASON: (?:\[')?[\w/]{0,}(\s)?[\w/]{0,}(?:'\])?

From the following input:

blah .... REASON: ['elm H1MM_rr'], blah ....
blah .... REASON: /lm H1MM_rr, blah ....
blah .... REASON: elm H1MM_rr.
blah .... REASON: ['elm H1MM_rr'].
blah .... REASON: [' '].

It will match:

REASON: ['elm H1MM_rr']
REASON: /lm H1MM_rr
REASON: elm H1MM_rr
REASON: ['elm H1MM_rr']
REASON: [' ']

You may find this documentation useful. You may also find this SO thread useful, if you'd like to modify the query to possibly make it more concise.

Community
  • 1
  • 1
oldboy
  • 5,729
  • 6
  • 38
  • 86
  • The elm H1MM_rr is just an example here. It could be any string after REASON: – user3407267 Jun 22 '18 at 16:21
  • @user3407267 ok, well, which characters would be allowed and which characters would not be allowed? – oldboy Jun 22 '18 at 16:56
  • It will be always preceded with REASON: – user3407267 Jun 22 '18 at 17:14
  • sometimes it will be with [' '] and sometime not but will always end with either . or , – user3407267 Jun 22 '18 at 17:15
  • @user3407267 yes, i know but which characters can be after reason and between the brackets and single quotes??? – oldboy Jun 22 '18 at 17:15
  • It would be a-z A-Z _ / – user3407267 Jun 22 '18 at 17:19
  • @user3407267 umm... in your question above, you also include `1`, and `.`??? ALSO, where is the cutoff? in other words, should a match also include `, blah ....` and or `.` and or `. ` at the end of each string? – oldboy Jun 22 '18 at 17:29
  • sorry it include 0-9 as well.. the cut off is either , or . – user3407267 Jun 22 '18 at 17:33
  • @user3407267 i've edited my answer accordingly. does it work in Java?? – oldboy Jun 22 '18 at 17:44
  • @user3407267 well? – oldboy Jun 22 '18 at 17:49
  • not exactly .. I am getting [ "REASON: ['elm H1MM_rr']", null ] – user3407267 Jun 22 '18 at 17:52
  • I tried - "REASON: (?:\\[')?[\\w/]{0,}(\\s)?[\\w/]{0,}(?:'\\])?" – user3407267 Jun 22 '18 at 17:52
  • @user3407267 um... ["**`REASON: ['elm H1MM_rr']`**", null] is the correct output is it not???? – oldboy Jun 22 '18 at 17:53
  • But with this :"REAOSN: (\\[')?s*([^,.]+)?s*([^,.]+)('\\])?" I am getting = [ "REAOSN: ['elm H1MM_rr']", "['", "elm H1MM_rr''", "]", null.string ] – user3407267 Jun 22 '18 at 17:54
  • Yeah..generally it will give me the expected string 2nd or 3rd element as the array.. I am trying to get tat string alone – user3407267 Jun 22 '18 at 17:55
  • @user3407267 you're not even using the query that i created for you. i'm sorry, but i've wasted enough time at this point trying to guess exactly what it is you're looking for. ["**`REASON: ['elm H1MM_rr']`**", null] seemed like legit output that matched what you said you were looking for... in that particular output the string is the first element in the array... likely `ele[0]` – oldboy Jun 22 '18 at 17:57
  • @user3407267 so `["REASON: ['elm H1MM_rr']", null]` is what you were looking for (right?), whereby in that particular output the string is the first element in the array, likely accessed by using something like `array[0]` – oldboy Jun 22 '18 at 17:59
  • Tats true but I am trying to get just elm H1MM_rr – user3407267 Jun 22 '18 at 19:04
  • @user3407267 what do u mean? also, what did you mean by **I tried REASON: ([^,. ]+)")but this still not working for ['']** in your original question? please edit your question and try to make it as clear as possible – oldboy Jun 22 '18 at 19:34