You can't do this with a single regex. (Well... maybe in Perl.)

(edit: Okay, you can do it with variable-length negative lookbehind, which it appears Java can (almost uniquely!) do; see Cyborgx37's answer. Regardless, imo, you shouldn't do this with a single regex. :))

What you can do is split the string into words and deal with each word individually. My Java is pretty terrible so here is some hopefully-sensible Python:

# Precompile some regex
looks_like_product_number = re.compile(r'\A[-0-9]+\Z')
not_wordlike = re.compile(r'[^a-zA-Z0-9]')
not_wordlike_or_hyphen = re.compile(r'[^-a-zA-Z0-9]')

# Split on anything that's not a letter, number, or hyphen -- BUT dots
# must be followed by whitespace
words = re.split(r'(?:[^-.a-zA-Z0-9]|[.]\s)+', string)

stripped_words = []
for word in words:
    if '-' in word and not looks_like_product_number.match(word):
        stripped_word = not_wordlike.sub('', word)
    else:
        # Product number; allow dashes
        stripped_word = not_wordlike_or_hyphen.sub('', word)

    stripped_words.append(stripped_word)

pass_to_lucene(' '.join(stripped_words))

When I run this with 'wal-mart 1-2-3', I get back 'walmart 1-2-3'.

But honestly, the above code reproduces most of what the Lucene tokenizer is already doing. I think you'd be better off just copying StandardTokenizer into your own project and modifying it to do what you want.

Answer from Eevee on Stack Overflow
Discussions

java - Remove every special character except hyphen - Stack Overflow
I have two Strings, for example "Stack Overflow" and "Stack-Overflow". Now I am working on a String matching method and I need to format some strings. Every special character should be the end of a More on stackoverflow.com
🌐 stackoverflow.com
regex - Remove all characters except - Code Review Stack Exchange
My code takes a string and replaces all characters which are not: English letters Numbers , / - I have tested it and it seems to generally work well enough. But it may have some catastrophic bug in... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
October 5, 2017
Need Regex Pattern: Can't start w num; No special characters except underscore and hyphen; allows characters/nums - Stack Overflow
Closest I've gotten: ^[-_[a-zA-Z0-9]*$ That still allows the string to start with numbers. Apologies for asking such question when there are resources everywhere. I just need something fast and ... More on stackoverflow.com
🌐 stackoverflow.com
January 24, 2011
Regex to check if string has special characters except dash and comma and split
If string contains special character like hello-world but not dash(/) and comma (,) then split get the string output which is input: hello-world output : world input : hi,hello output: hi,hello input : my-friend output: friend Thanks More on forum.uipath.com
🌐 forum.uipath.com
5
0
September 30, 2020
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Regular_expressions › Cheatsheet
Regular expression syntax cheat sheet - JavaScript | MDN
2 weeks ago - This page provides an overall cheat sheet of all the capabilities of RegExp syntax by aggregating the content of the articles in the RegExp guide. If you need more information on a specific topic, please follow the link on the corresponding heading to access the full article or head to the guide. Character classes distinguish kinds of characters such as, for example, distinguishing between letters and digits.
🌐
P768c62a69f0
ayo.p768c62a69f0.pw › regex-no-special-characters-except-hyphen.html
Regex no special characters except hyphen. validation rule to exclude special characters except (.) and (‘).
The :space: portion of the regex makes no sense, and probably does not do what you intend. In fact, inside the character class, ,-: means "all characters with ASCII codes from 44 the comma up to 58 the colon ". A literal hyphen must be the first or the last character in a character class; otherwise, it is treated as a range like A-Z.Social credit uk
🌐
Regex Tester
regextester.com › 93960
Name - no symbols or special chars. - Regex Tester/Debugger
Regular Expression to Underscore, hyphen allowed. No special chars (@! etc.). Can't end with space. Can't yet figure out how to only allow space and not \t\r\n.
🌐
Webmaster World
webmasterworld.com › php › 4603466.htm
Regex pattern cannot start or end with hyphen - PHP Server Side Scripting forum at WebmasterWorld - WebmasterWorld
In fact all of the hyphen-related rules work best as negatives. As a single rule: !(^|-)-|-$ or, probably more efficiently, !^-|-($|-) where a leading ! applies to the whole pattern. If you say [^-] then you open the door for non-word characters such as punctuation. I don't think you've ever explained what this RegEx-- we're now in the php forum-- is actually supposed to do.
Find elsewhere
🌐
CodeProject
codeproject.com › Questions › 788882 › Regex-pattern-for-alphanumeric-hyphen-and-undersco
https://www.codeproject.com/Questions/788882/Regex...
Disclaimer: References to any specific company, product or services on this Site are not controlled by GoDaddy.com LLC and do not constitute or imply its association with or endorsement of third party advertisers
🌐
UiPath Community
forum.uipath.com › help › studio
Regex to check if string has special characters except dash and comma and split - Studio - UiPath Community Forum
September 30, 2020 - If string contains special character like hello-world but not dash(/) and comma (,) then split get the string output which is input: hello-world output : world input : hi,hello output: hi,hello input : my-friend o…
🌐
Stack Overflow
stackoverflow.com › questions › 53273602 › js-regex-for-ignoring-all-special-characters-but-space-and-hyphen
JS Regex for ignoring all special characters but space and hyphen
Without understanding exactly what you're trying to achieve no answer here can be correct! ... You can use a character class that includes a-z, spaces, and hyphens, and check to see that the whole string is composed of only those characters, by repeating it from the start of the string (^) to the end ($):
🌐
NTU Singapore
www3.ntu.edu.sg › home › ehchua › programming › howto › Regexe.html
Regular Expression (Regex) Tutorial
Getting started with regex may not be easy due to its geeky syntax, but it is certainly worth the investment of your time. This section is meant for those who need to refresh their memory. For novices, go to the next section to learn the syntax, before looking at these examples. Character: All characters, except those having special ...
🌐
Medium
medium.com › @MynaviTechTusVietnam › regex-for-dummies-part-3-character-set-or-condition-and-word-boundary-assertion-3af99d7df907
Regex For Dummies: Part 3: Character Set, Or condition, and Word Boundary Assertion | by Mynavi TechTus Vietnam | Medium
October 15, 2023 - If you want to exclude all special characters, including underscores, the regex would be ^#[^\W_]+$. ... Indeed, for a valid email, we can only use characters a-z, A-Z, 0–9, +, ., _, %, and — before the ‘@’ symbol, and we can only use characters a-z, A-Z, 0–9, and — after the ‘@’ symbol. Therefore, in the example above, we used the metacharacter . to match any character, which is incorrect.
🌐
Openmolecules
openmolecules.org › help › regex.html
DataWarrior User Manual
All characters except the listed special characters match a single instance of themselves · Matches the character with the specified ASCII/ANSI value, which depends on the code page used. Can be used in character classes · Match an LF character, CR character and a tab character respectively.
🌐
Regular-Expressions.info
regular-expressions.info › charclass.html
Regexp Tutorial - Character Classes or Character Sets
In most regex flavors, the only special characters or metacharacters inside a character class are the closing bracket ], the backslash \, the caret ^, and the hyphen -. The usual metacharacters are normal characters inside a character class, and do not need to be escaped by a backslash.
🌐
Reddit
reddit.com › r/regex › remove unwanted hyphen from middle of word.
r/regex on Reddit: Remove unwanted Hyphen from middle of word.
March 17, 2022 -

Hi, I thought this would be an eayier task but after some googleing I did not find my answer. I found answers to problems where the string had specific Lenghts, mines do not. www.regex101.com/r/66D5Es/1 I used the answer I got for my previous problem, the purple dotted bars seem to indicate the string finds what it's upposed to but I cannot replace/remove the hyphen preceding it. Only lower case letters are important because IT- must not be included

🌐
The Full-Stack Blog
coding-boot-camp.github.io › full-stack › computer-science › regex-tutorial
Regular Expression Tutorial | The Full-Stack Blog
Now let’s look at how quantifiers are used in the “Matching a Username” regex. There, we have the quantifier {3,16}. This means that we want to find the preceding string pattern a minimum of 3 times and a maximum of 16 times. Because our bracket expression ([a-z0-9_-]) will match any string that includes any combination of lowercase letters between a and z, any number between 0 and 9, and the special characters of an underscore or a hyphen, this quantifier means that this string has to be between 3 and 16 characters.