I want to extract a code from an input string from different pages. Sample code is '110-PT-0988'.
我想从不同页面的输入字符串中提取代码。示例代码是“110 - pt - 0988”。
This RegExp fits other possible cases '^\d{3}-[A-Z]{1,6}-\d{4}[A-Z]{0,2}$'
.
这RegExp适合其他可能的情况下' ^ \ d { 3 } -[a - z]{ 1,6 } - \ d { 4 }[a - z]{ 0,2 } $’。
I want to return a string variable containing the code within the input string e.g. for an input string
我想返回一个字符串变量,其中包含输入字符串中的代码,例如输入字符串
'Code part: xx Code No: 120-PXT-2234X System Process .....xyz blah blah'.
代码部分:xx代码编号:120-PXT-2234X系统流程…xyz”。
I want the return string to be '120-PXT-2234X'
我希望返回字符串为'120-PXT-2234X'
2 个解决方案
#1
3
The problem are the anchors ^
and $
. They match the start and the end of the string and since your pattern is in the middle of a string, it will not find it.
这个问题是锚^和$。它们与字符串的开始和结束匹配,因为您的模式位于字符串的中间,所以它不会找到它。
The anchor \b
for a word boundary, would be a better choice here, try
一个单词边界的锚b,在这里是一个更好的选择,试试看
\b\d{3}-[A-Z]{1,6}-\d{4}[A-Z]{0,2}\b#
See it here on Regexr
在Regexr上看到它。
#2
0
If you want to match such a pattern within a larger string, then you need to remove the anchors (^
and $
):
如果你想匹配这样的模式在一个大的字符串,那么你需要删除锚(^和$):
\d{3}-[A-Z]{1,6}-\d{4}[A-Z]{0,2}
That way the regex matches any substring that matches the pattern.
这样,regex将匹配与模式匹配的任何子字符串。
#1
3
The problem are the anchors ^
and $
. They match the start and the end of the string and since your pattern is in the middle of a string, it will not find it.
这个问题是锚^和$。它们与字符串的开始和结束匹配,因为您的模式位于字符串的中间,所以它不会找到它。
The anchor \b
for a word boundary, would be a better choice here, try
一个单词边界的锚b,在这里是一个更好的选择,试试看
\b\d{3}-[A-Z]{1,6}-\d{4}[A-Z]{0,2}\b#
See it here on Regexr
在Regexr上看到它。
#2
0
If you want to match such a pattern within a larger string, then you need to remove the anchors (^
and $
):
如果你想匹配这样的模式在一个大的字符串,那么你需要删除锚(^和$):
\d{3}-[A-Z]{1,6}-\d{4}[A-Z]{0,2}
That way the regex matches any substring that matches the pattern.
这样,regex将匹配与模式匹配的任何子字符串。