1

Regarding this question, what about adding special characters to the mix? And also saving the spaces into the list as well.

Example

This is a string, that "will be" highlighted when your 'regular expression' matches something!!!

Gives me

This

is

a

string
,

that

"will be"

highlighted

when

your

'regular expression'

matches

something
!
!
!

But i wasn't able to make it work with special characters as well...

I tried (?=[^A-Za-z0-9])|(?<=[^A-Za-z0-9])|[\\s\"']+|\"[^\"]*\"|'[^']*', which results in

This

is

a

string ,

that

" will

be "

highlighted

when

your

' regular

expression '

matches

something
!
!
!

EDIT I managed to find the regex i needed myself

(\"[^\"]*\")|\\W|\\w+

This will split all words, whitespaces and special characters. It also keeps everything that are within double quotes as one element by itself

user3413646
  • 167
  • 2
  • 11
  • Which "special" characters did you use and why did it not work? What was the result? – The fourth bird Apr 17 '20 at 06:20
  • i added (?=[^A-Za-z0-9])|(?<=[^A-Za-z0-9]) into the pattern, i want to do for all special characters – user3413646 Apr 17 '20 at 06:23
  • What does the whole pattern look like that did not work? – The fourth bird Apr 17 '20 at 06:25
  • edited the question – user3413646 Apr 17 '20 at 06:35
  • Stating your question as a variant of a linked question is problematic and unnecessary, especially when the earlier question is quite short. It would be better to state a complete question, possibly mentioning that it is similar to the earlier one, giving the link. If the earlier question were deleted (perhaps not likely) your question would become a ship without an anchor. If readers have a question about the earlier question who are they to ask for clarification? Lastly, "...adding special characters to the mix?" and "...saving the spaces into the list as well." are vague. – Cary Swoveland Apr 17 '20 at 06:36
  • 1
    This part of the pattern `[\\s\"']+` is missing the leading `^` and should be `[^\\s\"']+` The two parts that you added to the alternation are lookaround assertions and are non consuming. See https://regex101.com/r/Pbi2Hn/1 where the matches are. It is still not clear what you are trying to achieve. – The fourth bird Apr 17 '20 at 06:41
  • can you provide an example of input and expected output – Abhinav Chauhan Apr 18 '20 at 12:10

0 Answers0