With the regular expression \s{2}\b I'm trying to replace the last two whitespaces before a word with a pipe (|) using a Word Boundary, but it's ignoring the number -18.055,81. With what kind of expression I can also get the last two whitespaces before -18.055,81?
Example:
1234 - This is a test 18.055,81 -18.055,81 0,00 0,00 18.055,81
Results in:
1234 - This is a test |18.055,81 -18.055,81 |0,00 |0,00 |18.055,81
What I want:
1234 - This is a test |18.055,81 |-18.055,81 |0,00 |0,00 |18.055,81
You can use
\s{2}(?=-?\d)
\s{2}(?=-?\w)
See the regex demo. Details:
\s{2} - two whitespace chars
(?=-?\d) - that are immediately followed with an optional hyphen and then a digit.
If there are any other word chars expected (letters, or underscores), replace \d with \w.
Related
I have to match a string that is 3-6 characters long, contains at least one letter, but can have letters, numbers and only 1 "-".
The "-" must not be at the start or at the beginning.
Match:
string
str-ng
st-ng
s1-1g
st-1g
Do not match:
strings
-string
string-
st--ng
s-tn-g
1111
st
The closest I've gotten is this:
^((?!-.*-)[0-9A-Z]{3,6})$
But this divides the regex match with - So it matches s-tri but not st-ri because there aren't 3 chars at each end
Maybe you can use:
^(?=.*[a-z])(?!-|.*-$|.*-.*-)[a-z\d-]{3,6}$
See the online demo
^ - Start string anchor.
(?=.*[a-z]) - Positive lookahead to make sure there is at least one letter.
(?!-|.*-$|.*-.*-) - Negative lookahead to prevent a hyphen at the beginning or at the end or multiple.
[a-z\d-]{3,6} - Three to six times a character from the give class.
$ - End string anchor.
Note that I used the case-insensitive flag.
You can use
^(?=.{3,6}$)(?=[^a-zA-Z]*[A-Za-z])[0-9a-zA-Z]+(?:-[0-9a-zA-Z]+)?$
See the regex demo. Details:
^ - start of string
(?=.{3,6}$) - string must contain three to six chars other than line break chars
(?=[^a-zA-Z]*[A-Za-z]) - there must be at least one ASCII letter in the string
[0-9a-zA-Z]+ - one or more alphanumeric ASCII chars
(?:-[0-9a-zA-Z]+)? - an optional sequence of - and then one or more alphanumeric ASCII chars
$ - end of string.
Looking at the pattern that you tried, you meant to exclude the match when there are 2 hyphens present using the negative lookahead.
Also this part [0-9A-Z]{3,6} does not match a hyphen.
Reading
The "-" must not be at the start or at the beginning.
You might do that using
^(?![^\n-]*-[^\n-]*-)(?=[^a-zA-Z\n]*[a-zA-Z])[a-zA-Z0-9][a-zA-Z0-9-]{2,5}$
Regex demo
If you meant also no - at the end:
^(?![^\n-]*-[^\n-]*-)(?=[^a-zA-Z\n]*[a-zA-Z])[a-zA-Z0-9][a-zA-Z0-9-]{1,4}[a-zA-Z0-9]$
Explanation
^ Start of string
(?![^\n-]*-[^\n-]*-) Assert not 2 times -
(?=[^a-zA-Z\n]*[a-zA-Z]) Assert a char a-zA-Z
[a-zA-Z0-9] Match One of the listed without -
[a-zA-Z0-9-]{1,4} Repeat 1-4 times any of the listed including -
[a-zA-Z0-9] Match One of the listed without -
$ End of string
Regex demo
To match a dash-less checksum I can do something like:
\b[0-9a-z]{32}\b
However, I'm seeing some checksums that also have dashes, such as:
d3bd55bf-062f-473b-9417-935f62c4c98a
While this is probably a fixed size, 8, then 4, then 4, then 4, then 12, I was wondering if I could do a regex where the number of non-dash digits adds up to 32. I think the answer is no, but hopefully some regex wizard can come up with something.
Here is a starting point for some sample inputs: https://regex101.com/r/K0IMKe/1.
You can use
\b[0-9a-z](?:-?[0-9a-z]){31}\b
See the regex demo.
It matches
\b - a word boundary
[0-9a-z] - a digit or a lowercase ASCII letter
(?:-?[0-9a-z]){31} - thirty-one repetitions of an optional - followed with a single digit or a lowercase ASCII letter
\b - a word boundary.
If you do not mind having a trailing - if there is a word char after it, at the end of a match, you may also use
\b(?:[0-9a-z]-?){32}\b
See this regex demo. Here, (?:[0-9a-z]-?){32} will match thirty-two repetitions of a digit or lowercase ASCII letter followed with an optional hyphen.
If there can be multiple dashes, you can assert 32 to 36 chars using a positive lookahead.
^(?=[a-z0-9-]{32,36}$)[a-z0-9]+(?:-[a-z0-9]+)*$
^ Start of string
(?=[a-z0-9-]{32,36}$) Positive lookahead, assert what is at the right is 32 - 36 repetitions of the listed characters
[a-z0-9]+ Match 1+ times any of the listed
(?: Non capture group
-[a-z0-9]+ Match a - followed by 1+ times any of the listed (the string can not end with a hyphen)
)* Close the group and match 0+ times to also match the string without dashes
$ End of string
Regex demo
If you want to limit the amount of dashes to 0 -4 times, you can change the quantifier * to {0,4}+
^(?=[a-z0-9-]{32,36}$)[a-z0-9]+(?:-[a-z0-9]+){0,4}+$
Regex demo
I cannot figure out how to add two regex together, I have these requirements:
Letters and space ^[\p{L} ]+$
Cannot be whitespace ^[^\s]+$
I cannot figure out how to write one regex that will combine both? There is perhaps some other solution?
You may use
^(?! +$)[\p{L} ]+$
^(?!\s+$)[\p{L}\s]+$
^\s*\p{L}[\p{L}\s]*$
Details
^ - start of string
(?!\s+$) - no 1 or more whitespaces are allowed till the end of the string
[\p{L}\s]+ - 1+ letters or whitespaces
$ - end of string.
See the regex demo.
The ^\s*\p{L}[\p{L}\s]*$ is a regex that matches any 0+ whitespaces at the start of the string, then requires a letter that it consumes, and then any 0+ letters/whitespaces may follow.
See the regex demo.
I'm trying a regex fro Alpha Numeric of length 7 (with positions 1,3,4 as characters and positions 2,5,6,7 as digits).
[a-zA-Z]|[0-9]|[a-zA-Z]|[a-zA-Z]|[0-9]|[0-9]|[0-9]
Can someone help me?
The sequence "character, digit, character, character, digit, digit, digit" is expressed in regex as
[a-zA-Z][0-9][a-zA-Z]{2}[0-9]{3}
If you're working in PCRE (with say, PHP):
^([a-zA-Z])([0-9])(?1){2}(?2){3}$
Breakdown:
^ - from the start of the string
([a-zA-Z]) - match and capture a single character in the ranges given: a-z, A-Z
([0-9]) - match and capture a single character in the ranges given: 0-9
(?1){2} - redo the regex in the first group twice (recursive subpattern)
(?2){3} - redo the regex in the second group 3 times (recursive subpattern)
$ - the end of the string
If you want to match this in the middle of a sentence, exchange ^ and $ for \b - which will match a word boundary
See the demo
If you're not using PCRE:
^[a-zA-Z][0-9][a-zA-Z]{2}[0-9]{3}$
Which does the same thing, but has some copy-paste involved
I checked on stackoverflow already but didn't find a solution I could use.
I need a regular expression to match any word (by word I mean anything between full spaces) that contains numbers. It can be alphanumeric AB12354KFJKL, or dates 11/01/2014, or numbers with hyphens in the middle, 123-489-568, or just plain normal numbers 123456789 - but it can't match anything without numbers.
Thanks,
Better example of what I want (in bold) in a sample text:
ABC1 ABC 23-4787 ABCD 4578 ABCD 11/01/2014 ABREKF
There must be something better, but I think this should work:
\S*\d+\S*
\S* - Zero or more non-whitespace characters
\d+ - One or more digits
\S* - Zero or more non-whitespace characters
Use this lookahead:
(?=\D*\d)
This asserts that the string contains any quantity of non numeric characters (\D) followed by a single digit.
If you want to match/capture the string, then just add .* to the regex:
(?=\D*\d).*
Reference: http://www.rexegg.com/regex-lookarounds.html