1

Hi I am new to the regex I am trying to match the pattern for bellow line

T071108452T    42D896D5O 3587

I want to match the pattern till T071108452T 42D896D5 and after this i want the Character "O" to match i tried something like this

 (T)(\d{9})(T)(\d{0,19}\s{0,19}\w{0,19})O

but it contains the "O" already with the \w{0,19} and i want to match "O" as specific character any help will be great thanks .

As for the more values are

T065000090T203 93 5797 9O 4037  
T325170628T0108626004D18O01054
T071108452T    42D896D5O 3587

So i want to match "T"-- then 9 digits then -- "T" and then any combination that is alphanumeric till --"O"

  • Is `42D896D5` always of the same maximum length? If so you can use `\w{0,8}(\w)` to match the last `O` in your example as a group. – Mike de Klerk Aug 05 '13 at 09:24
  • 1
    I'm not sure your question is very clear. Do you have more examples of patterns to match and to reject? – Jerry Aug 05 '13 at 09:29
  • 1
    Could you elaborate on what you are trying to match? `(\d{0,19}\s{0,19}\w{0,19})` matches 0-19 digits followed by 0-19 spaces followed by 0-19 word characters. I don't think that is what you want. – Boris the Spider Aug 05 '13 at 09:29
  • The first `\d{0,19)` in your example does nothing -- at least, not for your example. The `\s{0,19}` seems redundant -- are you *expecting* zero, up to 19 spaces? One would suggest `\s+` here -- one space or more. Finally, to make your last `\w{0,19}` *non*-greedy, use `\w{0,19}?` instead. (In addition: what's with the random list of tags? You simply added everything related to regex you could find?) – Jongware Aug 05 '13 at 09:31
  • I want to match the spaces,numbers and alphabets except "O" in the given pattern .. I am adding more values to get the pattern –  Aug 05 '13 at 09:34
  • @user2553512 This is how your current regex is working: [link](http://www.regex101.com/r/mQ1vN6). The first one does not match because of the spaces between the alphanumeric characters. But as far as I see, your regex is working as intended? – Jerry Aug 05 '13 at 09:42

1 Answers1

0

Say you have the following string:

  T071108452T    42D896D5O 3587O
Note that I added an extra O --^

If you want to match until the last O, you may use the following pattern: [\w\s]+(?=O)

This means:

  • [\w\s]+ match words and whitespaces one or more times, greedy.
  • (?=O) Zerowidth lookahead assertion to match until O found

Now if you want to match until the first occurence of O then you may use the following pattern: [\w\s]+?(?=O). Note the added question mark, it's to match ungreedy.

Note: \w will also match an underscore, you may replace [\w\s] by [^\W\S_] to prevent that. Note the negation and the uppercased letters.

HamZa
  • 14,671
  • 11
  • 54
  • 75