Find Regex for states in US having this pattern - regex

Regex for us state
I want to retrieve state in this string. there is two types.
My string having these types.
US-VA-Arlington
VA-Arlington
In above from these i want to get state(VA) every time.
Please send suggestions.
Thanks,
Girish

Try following regex
([^-]*)-[^-]*$
Required state will be captured in \1

Try with following regex:
([A-Z]+)-\w+$

Use this regexp:
^(?:[A-Z]{2}-)?([A-Z]{2})-
The first optional group will match the country code if it exists; but it's a non-capturing group. The second group matches the state code. The state will be in capture group 1.

(US\-)?(\w\w)\-(\w+)
The first group collects 0 or 1 instances of US-
The second group collects the state abbreviation
The third group collects the city name - you may have to modify this regex to accept spaces (as others pointed out)

Related

Using regex to extract a string including a space between other strings and whitespace

I have the following output from my wireless controller and also the regex statement below. I am trying to parse out the various values using regex named capturing groups. The space in the 'Global/whatever Lab/Lab01' value is throwing everything after that value off. Is there a way to repalce the \S+ after the group to capture the whole value of 'Global/whatever Lab/Lab01'? Thank you.
Number of APs: 2\nAP Name Slots AP Model Ethernet MAC Radio MAC Location Country IP Address State \nAPAC4A.56BE.18A0 2 9120AXI ac4a.56be.18a0 045f.b91a.0a40 Global/whatever Lab/Lab01 US 2.2.2.2 Registered \nAPHAV-LAB-TEST-01 2 9120AXI ac4a.56be.8cd4 045f.b91d.4ce0 default location US 1.1.1.1 Registered
(?P<ap_name>\S+)\s+(?P<slots>\d+)\s+(?P<model_number>\S+)\s+(?P<ether_mac>\S+)\s+(?P<radio_mac>\S+)\s+(?P<location>\S+)\s(?P<country>\S+)\s+(?P<ip_address>\S+)?\s+(?P<state>\S+)
When you need to match a multi-word field value, make sure you can describe the format of the field(s) next to it. Once you know the rules, you can match the "unknown" field with a mere .*? pattern.
See an example solution:
(?P<ap_name>\S+)\s+(?P<slots>\d+)\s+(?P<model_number>\S+)\s+(?P<ether_mac>\S+)\s+(?P<radio_mac>\S+)\s+(?P<location>.*?)\s+(?P<country>[A-Z]{2,})(?:\s+(?P<ip_address>\d{1,3}(?:\.\d{1,3}){3}))?\s+(?P<state>\S+)
See the regex demo.
Now, the location group pattern is (?P<location>.*?) and it matches any char, 0 or more occurrences but as few times as possible, other than line break chars, and it is possible here since the next group pattern, country group, is now (?P<country>[A-Z]{2,}) and matches any substring of two or more uppercase ASCII letters.
Note I also "spelled out" the ip_address group pattern and made the whole part with initial whitespaces optional, (?:\s+(?P<ip_address>\d{1,3}(?:\.\d{1,3}){3}))?.
maybe try doing something similar to replacing \S+ with [\S ]+
I don't know if there's anything like an escape code you can use to represent the space between the []

Extracting String using regex

I am using a HTA Application I wrote for our help desk to take notes.
I've been using regex (Best I can) to CTRL+A our ticket pop up and click parse on my app to fill out information
I need to find "TICKET - T00000000.0000 - Account Security (Company Name...)" and only grab the "Account Security" section. or for future grab whatever is between the 2nd - and the (
Any suggestions would be grand
here is an example what I've tried and what I am using
try {
$(".problem_description", context).val(clipdata.match(/TICKET -.+[)]/)[0]);
}
catch (e) {
}
Update
I have tried a few of the suggestions here but the results still seem to give me the entire string or error out in my script.
Here's the regex using positive lookbehind:
(?<=TICKET\ -\ T\d{8}\.\d{4}\ -\ ).*\)
Here's regex101 explanation: https://regex101.com/r/6BN16e/1
The query effectively says matching anything after "TICKET - T(8 digits).(4 digits) - ". You can of course tweak it to your specification.
Here's a tutorial on lookahead and lookbehind that may be helpful: https://www.regular-expressions.info/lookaround.html
Use a capture group. In a regex you can use parentheses to mark a capture group. So if you define a pattern where a portion of it marks the text you want to extract, you can wrap that portion in parentheses. The object returned by the match function in most languages is an object that lets you access the values of individual capture groups.
Try this regex I quickly made up: /[^-]*-[^-]*- ([^(]*)/
Full example: var matches = "TICKET - T00000000.0000 - Account Security (Company Name...)".match(/[^-]*-[^-]*- ([^(]*)/)
Your value will be in matches[1].
It says: start from the beginning, look for anything not a dash, then a dash, then anything not a dash, then another dash, then a space, then capture anything not a left-parenthesis into a capture group.
This one will leave an extra space at the end of the captured group value. Also, it will truncate your value if your value contains a left parenthesis.

RegEx capture group that excludes a specific pattern

I am trying to come up with a RegEx pattern that takes strings that look like this:
KEEP_THIS_L_1234
KEEP_THIS_R_12
KEEP_THIS
and returns a capture group with this result:
KEEP_THIS
KEEP_THIS
KEEP_THIS
So far I have tried /^(\w+)(?=_(L|R))(?=_\d{0,4})/, but this pattern only returns the capture groups for the first two instances:
KEEP_THIS
KEEP_THIS
Can someone help me understand what I am missing?
Thanks!
you need to make the last two groups optional, like this:
/^(\w+?)((_(L|R))(_\d{0,4}))?$/
Your desired result will always be in $1.
This has the advantage that your other data captured (if any) will be in groups $2 and $3.
Change your regex like below.
^(\w+)(?=(?:_[LR]_\d{0,4})|$)
DEMO

How to skip phrase only if it exists

I've been cutting my teeth on regex over the past couple of days, and have encountered an issue I cant seem to get past.
Lets assume the following 3 string values
AKA NAME:FOO
FOO
AKA NAME:
My goal is to capture the value of the string after AKA NAME: in a named match group, and if AKA NAME: is not present, capture the entire string in the match group. If "AKA NAME:" IS present with no subsequent value, the regex expression should fail. I have developed the following expression
^(?:AKA NAME:)?\s*(?<VALUE>(.|\n|\r){1,225})$
This will correctly capture the word "FOO" in the first 2 strings above, however, in the third it captures "AKA NAME:" in the match group. I figured putting ? after the non-capture group containing "AKA NAME:" would have caused the engine to skip this value, but it is not.
Can someone give me some guidance?
You can try with:
(?:AKA NAME:)*(.+)*
and check if $1 exist.
DEMO
Use a look behind assertion and then exclusion set for "AKA NAME:" only:
EDITTED:
(?<=AKA NAME:)\s?(\w+)|(?!AKA NAME:)^(\w+)
DEMO
I think you can use this regex:
"^(AKA NAME\s*:)?\s*(.*)$"gm
and get \2 for your result.
[Regex Demo]
^(AKA NAME:)?(.*)$
\2 should contain what you're looking for.

regex to find value at a particular location

Presently the regex is:
[A-Z]+(?=-\d+$)
This pulls out the correct value for most of the strings which follow the below format:
ANG-RGN-SOR-BCP-0004 i.e. BCP
However it pulls out SS for the following document instead of PMR:
ANG-B31-OPS-PMR-MACE-SS-0229
So basically I want to pull out the fourth term (between the hyphens), so it should pick BCP and PMR.
The following regex will get the 4th item in group 1:
(?:[A-Z0-9]+-){3}([A-Z0-9]+)
The first bit in (?:...) is a "non-capturing group" which acts like a group but won't appear in the backreference list.
The next bit means "3 of these non-capturing groups".
And finally, a capturing group to collect what you want.
I have assumed here that all the groups contain only uppercase letters and digits, you should modify the parts in [square brackets] to represent what these groups could be.
A more easily understandable method in Python:
a = "ANG-B31-OPS-PMR-MACE-SS-0229"
part = a.split('-')[3]
print part
This gives "PMR".
This should suit your needs (demo):
(?:.+?-){3}([^-]+)
You'll be able to access the fourth term in the first capturing group.