Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for whitespaces with RegEx

Tags:

regex

I have strings that look like some text - other text and I need to delete everything before and including the hyphen - and the space after it

But do to typos I might have : some text -other text or some text- other text or some text-other text or double spaces instead of single spaces

I am using RegEx ^.*\s+\-\s+ and this works for some text - other text with single or multiple spaces before and after the -

But for the other possibilities where the whitespace is missing, I have used two or so I have ^.*\s+\-\s+|.*\-\s|.*\-

Is there a more concise patter that does not use multiple ors for this?

Thank you for any help on this

https://regex101.com/r/TNU7i6/1

like image 828
xyz333 Avatar asked Jun 26 '26 08:06

xyz333


2 Answers

Instead of using an alternation with 3 patterns, you might use a pattern to match all except the -, then match the - and optional whitespace chars.

^[^-]*-\s*

Regex demo

If there should be a non whitespace char following, and a lookahead is supported:

^[^-]*-\s*(?=\S)
  • ^ Start of string
  • [^-]*- Match 0+ times any char except -, then match -
  • \s* Match optional whitespace chars
  • (?=\S) Positive lookahead, assert a non whitespace char to the right

Regex demo

Note that \s and the negated character class [^-] can also match a newline.

like image 130
The fourth bird Avatar answered Jun 27 '26 23:06

The fourth bird


1st solution: With your shown samples, please try following.

^.*?\s+\S+\s?-\s*(.*)$

OR

^.*?\s+\S+\s*-\s*(.*)$

Online demo for above regex



2nd solution: You could use \K option too to forget matched regex part, in that case try:

^.*?\s+\S+\s?-\s*\K.*$

OR

^.*?\s+\S+\s*-\s*\K.*$

Online demo for above regex

1st solution explanation:

^.*?\s+  ##From starting of value matching till 1st occurrence of space(s).
\S+\s?   ##Matching 1 or more non-space occurrences followed by optional space here.
-\s*     ##Matching - followed by optional space.
(.*)$    ##Matching everything till last of value.

2nd solution explanation:

^.*?\s+  ##Matching everything till 1st space occurrence(s) from starting of value.
\S+\s?   ##Matching non spaces 1 or more occurrences followed by space optional.
-\s*\K   ##Matching - followed by spaces(0 or more occurrences) and \K will discard all previous matched values(so that we can match exact values as per output).
.*$      ##Matching everything after previously matched values(which is discarded by \K).
like image 21
RavinderSingh13 Avatar answered Jun 27 '26 22:06

RavinderSingh13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!