正则表达式,用于捕获特定数字

时间:2019-05-22 03:46:40

标签: java regex regex-lookarounds regex-group regex-greedy

从下面的日志中,我如何单独grep'951792'值

atomic_int

我尝试了Java拆分/子字符串操作。但是代码行很高。使用正则表达式如何获取'atomic_fetch_add_explicit( &primeCount, localTally, memory_order_relaxed );'值

输出将为

2019 May 22 03:32:17.952296 france1v4 sh[4937]: 190522-03:32:17.951792 [mod=REC, lvl=INFO] [tid=26130] Recording A8602096210405800406L200218680503121519 size is 4145956224 bytes
2019 May 22 03:32:17.952387 france1v4 sh[4937]: 190522-03:32:17.951895 [mod=REC, lvl=INFO] [tid=26130] RecordingInfo = fffocap://0x401e
2019 May 22 03:32:17.952466 france1v4 sh[4937]: 190522-03:32:17.951934 [mod=REC, lvl=INFO] [tid=26130] recording_dvr_from_recording_info:physicalSegmentCount=10   

3 个答案:

答案 0 :(得分:3)

在这里,我们可能只想在所需数字旁边使用正确的边界[mod并收集我们第一个捕获组中的数字,也许与此类似:

([0-9]+)\s\[m 

如果我们愿意,我们可以添加更多边界,例如:

(.+?)([0-9]+)\s\[m.+

enter image description here

DEMO

测试

import java.util.regex.Matcher;
import java.util.regex.Pattern;

final String regex = "(.+?)([0-9]+)\\s\\[m.+";
final String string = "2019 May 22 03:32:17.952296 france1v4 sh[4937]: 190522-03:32:17.951792 [mod=REC, lvl=INFO] [tid=26130] Recording A8602096210405800406L200218680503121519 size is 4145956224 bytes\n"
     + "2019 May 22 03:32:17.952387 france1v4 sh[4937]: 190522-03:32:17.951895 [mod=REC, lvl=INFO] [tid=26130] RecordingInfo = fffocap://0x401e\n"
     + "2019 May 22 03:32:17.952466 france1v4 sh[4937]: 190522-03:32:17.951934 [mod=REC, lvl=INFO] [tid=26130] recording_dvr_from_recording_info:physicalSegmentCount=10   \n";
final String subst = "\\2";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);

System.out.println("Substitution result: " + result);

演示

const regex = /(.+?)([0-9]+)\s\[m.+/gm;
const str = `2019 May 22 03:32:17.952296 france1v4 sh[4937]: 190522-03:32:17.951792 [mod=REC, lvl=INFO] [tid=26130] Recording A8602096210405800406L200218680503121519 size is 4145956224 bytes
2019 May 22 03:32:17.952387 france1v4 sh[4937]: 190522-03:32:17.951895 [mod=REC, lvl=INFO] [tid=26130] RecordingInfo = fffocap://0x401e
2019 May 22 03:32:17.952466 france1v4 sh[4937]: 190522-03:32:17.951934 [mod=REC, lvl=INFO] [tid=26130] recording_dvr_from_recording_info:physicalSegmentCount=10   
`;
const subst = `$2`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

RegEx

如果不需要此表达式,可以在regex101.com中对其进行修改或更改。

RegEx电路

jex.im可视化正则表达式:

enter image description here

答案 1 :(得分:1)

您可以尝试以下正则表达式:

(?<=[0-9]{6}-[0-9]{2}:[0-9]{2}:[0-9]{2}\.)[0-9]+

在添加Java代码时,不要忘记将.\\.)进行两次转义。

输入:

2019 May 22 03:32:17.952296 france1v4 sh[4937]: 190522-03:32:17.951792 [mod=REC, lvl=INFO] [tid=26130] Recording A8602096210405800406L200218680503121519 size is 4145956224 bytes
2019 May 22 03:32:17.952387 france1v4 sh[4937]: 190522-03:32:17.951895 [mod=REC, lvl=INFO] [tid=26130] RecordingInfo = fffocap://0x401e
2019 May 22 03:32:17.952466 france1v4 sh[4937]: 190522-03:32:17.951934 [mod=REC, lvl=INFO] [tid=26130] recording_dvr_from_recording_info:physicalSegmentCount=10 

匹配项:

951792
951895
951934 

Demo 1

对于使用前瞻性和后瞻性的更严格的正则表达式,请使用:

(?<=[0-9]\]:\s[0-9]{6}-[0-9]{2}:[0-9]{2}:[0-9]{2}\.)[0-9]+(?=\s\[mod=REC)

Demo 2

java代码示例:

String input = "2019 May 22 03:32:17.952296 france1v4 sh[4937]: 190522-03:32:17.951792 [mod=REC, lvl=INFO] [tid=26130] Recording A8602096210405800406L200218680503121519 size is 4145956224 bytes\n" + 
                "2019 May 22 03:32:17.952387 france1v4 sh[4937]: 190522-03:32:17.951895 [mod=REC, lvl=INFO] [tid=26130] RecordingInfo = fffocap://0x401e\n" + 
                "2019 May 22 03:32:17.952466 france1v4 sh[4937]: 190522-03:32:17.951934 [mod=REC, lvl=INFO] [tid=26130] recording_dvr_from_recording_info:physicalSegmentCount=10   ";
List<String> matches = new ArrayList<String>();
Matcher m = Pattern.compile("(?<=[0-9]{6}-[0-9]{2}:[0-9]{2}:[0-9]{2}\\.)[0-9]+")
.matcher(input);
while (m.find()) {
    matches.add(m.group());
}
System.out.println(matches);

代码输出:

[951792, 951895, 951934]

答案 2 :(得分:1)

//逐行循环循环。

<form method='POST' action='' class="col s6" enctype="multipart/form-data"> {% csrf_token %}

    <!-- 4 different basic 4 approaches here -->

    <!-- {{form}} /// Radio buttons don't display and can't tell which is selected -->

    <!-- {{form.as_p}} /// Radio buttons don't display and can't tell which is selected -->

    <!-- {{form.as_table}} /// Radio buttons don't display and can't tell which is selected -->


    <!-- {{form.as_ul}} /// Radio buttons don't display and can't tell which is selected -->




  <!-- Slightly altered version I found on the Django Documentation -->
<!-- ////This is selectable, but doesn't push to the model -->
    <div class="fieldWrapper">  
            {{ form.month.errors }}
    <label>Month:</label>
    <br>
        {% for month, monthb in form.fields.month.choices %}
            <label>
             <input name="group1" type="radio" />
             <span> {{month}} </span>
            </label>
        {% endfor %}
    </div>




    <!-- /// Alternate approach to one above. Can't select the radio buttons, but each displays on screen as a radio button -->
    <div>  
        {% for a, b in form.fields.month.choices %}
        <input type="radio" name="phone" id="id_month">
        <span>{{a}}</span>
        {% endfor %} 


        <!-- /// I noticed that when I do it this way, if the label starts and ends next to itself, then it won't dispaly all 12 months as a radio button. -->
<div class="fieldWrapper">  
    {{ form.month.errors.as_text }}   
    <!-- ///// Also did this not using the as_text, but that had not change in affect -->
    {% for month, monthb in form.fields.month.choices %}
   <label>
  <input value='month' type="radio" />
     <span> {{month}} </span>
    </label>
    {% endfor %}
</div>




<div class="div right-align">
<button class="btn waves-effect waves-light" type="submit" name="action" value='Save'>Submit</button>
</div>

</div>
              </form>

在此处获取正则表达式参考:https://regex101.com/r/8F0D4w/1

相关问题