0
input = 
6:/BENM/Gravity Exports/REM//INV: 3267/FEB20:65:ghgh
6:/BENM/Tabuler Trading/REM//IMP/2020-341

original_regex = 6:[A-Za-z0-9 \/\.\-:] - bt this is taking full string 6:/BENM/Gravity Exports/REM//INV: 3267/FEB20:65:ghgh

modified_regex_pattern = 6:[A-Za-z0-9 \/\.\-:]{1,}[\/-:]

In the first string i want output till 6:/BENM/Gravity Exports/REM//INV: 3267/FEB20 but its giving till :65: Can anyone suggest better way to write this. Example as below https://regex101.com/r/pAduvy/1

1 Answer 1

1

You could for example use a capturing group with an optional part at the end to match the :digits:a-z part.

(6:[A-Za-z0-9 \/.:-]+?)(?::\d+:[a-z]+)?$
  • ( Capture group 1
    • 6:[A-Za-z0-9 \/.:-]+? Match any of the listed in the character class as least as possible
  • ) Close group 1
  • (?::\d+:[a-z]+)? optionally match the part at the end that you don't want to include
  • $ End of string

Regex demo

Note Not sure if intended, but the last part of your pattern [\/-:] denotes a range from ASCII range 47 - 58.


Or a more precise pattern to get the match only

6:/\w+/\w+ \w+/[A-Z]+//[A-Z]+(?:: \d+)?/[A-Z]*\d+(?:-\d+)?
  • 6:/\w+/\w+ Match 6 and 2 times / followed by 1+ word chars and a space
  • \w+/[A-Z]+//[A-Z]+ Match 1+ word chars, / and uppercase chars, // and again uppercase chars
  • (?:: \d+)? Optionally match a space and 1+ digits
  • /[A-Z]*\d+ Match /, optional uppercase chars and 1+ digits
  • (?:-\d+)? Optionally match - and 1+ digits

Regex demo

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks , but cant hard code like this because there are cases where we may not have some character. string can be "/BENM/Tabuler Trading" just like this. problem i m facing with multiple :(colon), :6, :65: are tags in this string , when i m including : in regex , its taking :65: tag also, i need data of tag 6 only ,that's the problem
@Navneet You can take from the first tag until the first occurrence of the next tag ^6:.*?(?=:\d|$) regex101.com/r/wpikoU/1 Or if the tag is optional ^(?:^6:)?.*?(?=:\d|$) regex101.com/r/pitdfR/1 If the 6 should not be hardcoded, you can use \d+ instead to match 1+ digits.
@Navneet Any feedback?
sorry, couldn't update, code is getting complex, let me run through this regex with all strings, bt for sample its working, Thanks
Accepting this as of now , Thanks @The fourth bird

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.