vb.net RegEx replace - regex

I have a regex that searches an input string looking for a possible SSN. That part all works great, but I want to be able to replace what I detect as a SSN with a string of asterisks.
For example, if 123456789 is my SSN to replace and I use "123456789, 00123456789000, 1234567899999" as the input string, I just want to end up with "*********, 00123456789000, 1234567899999" but everything I am trying is affecting the second and third string elements as well.
I was thinking that I could use my initial search pattern as the same replace pattern but also make sure there wasn't a digit on each side of it but I can't get it to work.
This is my search pattern and it works fine:
Dim reg As New Regex("\d{3}\D{0,1}\d{2}\D{0,1}\d{4,}")

You can make sure there are word boundaries before and after the pattern using \b...
"\b\d{3}\D{0,1}\d{2}\D{0,1}\d{4}\b"

Related

Regex match last substring among same substrings in the string

For example we have a string:
asd/asd/asd/asd/1#s_
I need to match this part: /asd/1#s_ or asd/1#s_
How is it possible to do with plain regex?
I've tried negative lookahead like this
But it didn't work
\/(?:.(?!\/))?(asd)(\/(([\W\d\w]){1,})|)$
it matches this '/asd/asd/asd/asd/asd/asd/1#s_'
from this 'prefix/asd/asd/asd/asd/asd/asd/1#s_'
and I need to match '/asd/1#s_' without all preceding /asd/'s
Match should work with plain regex
Without any helper functions of any programming language
https://regexr.com/
I use this site to check if regex matches or not
here's the possible strings:
prefix/asd/asd/asd/1#s
prefix/asd/asd/asd/1s#
prefix/asd/asd/asd/s1#
prefix/asd/asd/asd/s#1
prefix/asd/asd/asd/#1s
prefix/asd/asd/asd/#s1
and asd part could be replaced with any word like
prefix/a1sd/a1sd/a1sd/1#s
prefix/a1sd/a1sd/a1sd/1s#
...
So I need to match last repeating part with everything to the right
And everything to the right could be character, not character, digit, in any order
A more complicated string example:
prefix/a1sd/a1sd/a1sd/1s#/ds/dsse/a1sd/22$$#!/123/321/asd
this should match that part:
/a1sd/22$$#!/123/321/asd
Try this one. This works in python.
import re
reg = re.compile(r"\/[a-z]{1,}\/\d+[#a-z_]{1,}")
s = "asd/asd/asd/asd/1#s_"
print(reg.findall(s))
# ['/asd/1#s_']
Update:
Since the question lacks clarity, this only works with the given order and hence, I suppose any other combination simply fails.
Edits:
New Regex
reg = r"\/\w+(\/\w*\d+\W*)*(\/\d+\w*\W*)*(\/\d+\W*\w*)*(\/\w*\W*\d+)*(\/\W*\d+\w*)*(\/\W*\w*\d+)*$"

How to extract big mgrs using regex

I have an input json:
{"id":12345,"mgrs":"04QFJ1234567890","code":"12345","user":"db3e1a-3c88-4141-bed3-206a"}
I would like to extract with regular expression MGRS of 1000 kilometer, in my example result should be: 04QFJ1267
First 2 symbols always digits, next 3 always chars and the rest always digits. MGRS have a fix length of 15 chars at all.
Is it possible?
Thanks.
All you really need to do is remove characters 8-10 and 13-15. If you want/need to do that using regex, then you could use the replace method with regex: (EDIT Edited to remove the rest of the string).
.*?(\w{7})\d{3}(\d{2})\d+.*
and replacement string:
$1$2
I see now you are using Java. So the relevant code line might look like:
resultString = subjectString.replaceAll(".*?(\\w{7})\\d{3}(\\d{2})\\d+.*", "$1$2");
The above assumes all your strings look like what you showed, and there is no need to test to be sure that "mgrs" is in the string.

Regex substring

I'm trying to select a substring using regex and I'm going round in circles. I need to select everything before the first "_".
exampale URL - GI_2013_JUNE_10_VOL3_LASTCHANCE
So the result Im looking for from the URL above would be "GI". The text before the first "_" can vary in length.
Any help would be much apprecited
The regex would be:
^[^_]+
and grab the whole regex match. But as a comment says, using a substring function is more efficient!
^[^_]*
...is the expression you're looking for.
It basically says: Select everything that is not an underscore, starting at the beginning of the string.
http://regexr.com?356in

replace all text before and after a certain string using a regular expression

I have the following string:
"Text before the string I want to get. Description: This is the string I want to keep. Rating: 5 stars."
From this string I want to cut of the first part and the last part. Only the middle is what I need. The program I use to load these strings can do a RegEx "Search and Replace". So I can replace the first and last part of the string with a space to empty these parts.
The words Description and Rating are always present in this form, so can be used to create the RegEx.
Try replacing matches of the following regex with an empty string (or a space):
^.*?Description:|Rating:.*$
This may have some unexpected behavior if "Text before the string I want" can also contain "Description:" or the string you want to keep can contain "Rating:".
Note that technically the anchors (^ and $) are not necessary here, but they are still useful to make it clear that everything from the beginning of the string up to "Description:" is being removed, as well as everything from "Rating:" until the end of the string.
if your tool/programming supports look-around:
'(?<=Description: ).*(?=Rating: )'
will give you the middle part. so that you don't have to "replace". just extract the matched part.

regex match string replace in new string (notepad )

In Notepad++ I need to match "dog" (search) in
<tag>old-string/dog.swf">more-old-string</tag>
then use a back-reference (\1) to include it in another string (replace):
new-string_\1>more-new-string
to give the result
new-string_dog.swf">more-new-string
I'm new to regex, so please show me how to do this first by matching "dog", then by excluding all old-string in the result.
Edit: I realize this might be confusing, so I posted the actual problem here: regex find word in string, replace word in new string (using Notepad++). I hope it makes more sense.
I am totally going off a limb here trying to understand what you want, but if I have understood you correctly you want to:
match a certain word in one string
create a new string with data from the old one, under the old string which should be untouched
If so, this regular expression: /^(.*?/([^"]+)\.swf)(".*$)/i with this replacement: \1\3\r\n<test>\2</test> should do the trick. I have used <test></test> to show you where you put your "new-string" stuff.
I hope this helps!