I am looking for a regular expression which removes text after a separator and removes the separator as well.
I tried .*&
to remove text after &
. This is working but I want to remove &
from my string as well.
Like &
, I have multiple other separators eg , ; \\ / - | (JT)
, ; \\ / - | (JT)
How can I achieve this?
With .*&
you're matching the text until and including the last &
in your text. If you don't want to include that in the match, put it in a positive lookahead assertion :
.*(?=&)
To look for more than one separator, use alternation and a character class :
.*(?=[&,;\\/|-]|\(JT\))
Note that this will change something like
First, Middle & Last
to
First, Middle
because the match stops before the last separator. If you want it to stop at the first separator, use a lazy quantifier :
.*?(?=[&,;\\/|-]|\(JT\))
Note that you will need to escape the backslashes for the string processor, so that regex, written as a string, will look like
".*?(?=[&,;\\\\/|-]|\\(JT\\))"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.