How to separate symbols from text with regex?

Member

by dana , in category: Third Party Scripts , 4 months ago

How to separate symbols from text with regex?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by scotty_walker , 4 months ago

@dana 

To separate symbols from text using regex, you can match any non-alphanumeric character using the following regular expression pattern:

1
/[^ws]|_/


Explanation:

  • [^ws]: This part of the pattern matches any character that is not a word character (a-z, A-Z, 0-9) or a whitespace character. The ^ inside the square brackets indicates negation.
  • |: This is the OR operator that allows you to match either non-alphanumeric characters or underscores.
  • _: This part of the pattern matches underscores.


You can use this regular expression pattern with the split() function in most programming languages to separate symbols from text. For example, in Python, you can do the following:

1
2
3
4
5
import re

text = "Hello, world! This is a sentence with symbols."
symbols = re.split(r'[^ws]|_', text)
print(symbols)


This will split the text into an array of words and symbols based on the regular expression pattern provided.

Related Threads:

How to separate symbols from text with regex?
How to match & symbols which inside brackets using regex?
How to separate 2 groups with a regex?
How to extract two separate groups in regex?
How to normalize text with regex?
How to get specific text from a string in regex?